Код: Выделить всё
public abstract class Exam
{
public int Id { get; set; }
public string Description { set; get; }
public abstract ICollection GetQuestions();
public abstract void SetQuestions(ICollection questions);
}
public abstract class Question
{
public int Id { get; set; }
public string Description { set; get; }
public abstract Exam getExam();
public abstract void setExam(Exam exam);
}
Вот мои конкретные классы экзамена:
Код: Выделить всё
[Table("SingleExam")]
public class SingleExam : Exam
{
public virtual ICollection Questions { get; set; }
public override ICollection GetQuestions() { return Questions as ICollection; }
public override void SetQuestions(ICollection questions)
{
if (!(questions is ICollection))
throw new ArgumentException("You must set single questions.");
Questions = questions as ICollection;
}
}
[Table("MultipleExam")]
public class MultipleExam : Exam
{
public virtual ICollection Questions { get; set; }
public override ICollection GetQuestions() { return Questions as ICollection; }
public override void SetQuestions(ICollection questions)
{
if (!(questions is ICollection))
throw new ArgumentException("You must set multiple questions.");
Questions = questions as ICollection;
}
}
Код: Выделить всё
[Table("SingleQuestion")]
public class SingleQuestion : Question
{
public int ExamId { get; set; }
public virtual SingleExam Exam { get; set; }
public override Exam getExam() { return Exam; }
public override void setExam(Exam exam)
{
if (!(exam is SingleExam))
throw new ArgumentException("You must set a SingleExam");
Exam = exam as SingleExam;
}
}
[Table("MultipleQuestion")]
public class MultipleQuestion : Question
{
public int ExamId { get; set; }
public virtual MultipleExam Exam { get; set; }
public override Exam getExam() { return Exam; }
public override void setExam(Exam exam)
{
if (!(exam is MultipleExam))
throw new ArgumentException("You must set a MultipleExam");
Exam = exam as MultipleExam;
}
}
p>
Есть ли лучший способ гарантировать, что подкласс класса «A» содержит или имеет определенный подкласс класса «B» (как в случае с моими экзаменами и вопросами) и имеет доступ к нему через абстрактный класс без абстрактных геттеров и сеттеров?
Подробнее здесь: https://stackoverflow.com/questions/400 ... s-property