有什么方法可以使实现接口 Ae 的每个 class 都有一个具体的 class 列表,这些 class 将 Ba 实现为 属性?

Is there any way to make each class that implements interface Ae has a List of one of the concrete classes that implement Ba as property?

有没有办法让每个实现接口 Ae 的 class 都有一个具体的 class 列表,其中一个实现 Ba 为 属性?

public interface IQuestion
{
    IAnswerOption[] Answers { get; set; }
}

public interface IAnswerOption
{
    int Id { get; }
    bool IsCorrect { get; set; }
}

public class AnswerOptionText : IAnswerOption
{
    public int Id { get; }
    public bool isCorrect;
    public string ansText;
}

public class AnswerOptionImage : IAnswerOption
{
    public int Id { get; }
    public bool isCorrect;
    public string imgSlug;
}

public class AudioQuestion : IQuestion
{
    public AnswerOptionImage[] Answers;
    public string audioName;
}

public class TextQuestion : IQuestion
{
    public AnswerOptionText[] Answers { get; set; }
    public string questionText { get; set; }
}

当我尝试时,AudioQuestionTextQuestion 不允许我使用 AnswerOptionImage[]AnswerOptionText[] 分别。

Visual Studio说我需要实现接口成员IQuestion.Answers,但这不是我想要的。

如果有人能帮助我,我将不胜感激。谢谢

您的 IQuestion 界面使用 generics 似乎很合适:

public interface IQuestion<T> where T: IAnswerOption
{
    T[] Answers { get; set; }
}

public class AudioQuestion : IQuestion<AnswerOptionImage>
{
    public AnswerOptionImage[] Answers{ get; set; }
    public string audioName;
}

public class TextQuestion : IQuestion<AnswerOptionText>
{
    public AnswerOptionText[] Answers { get; set; }
    public string questionText { get; set; }
}