C#不同类别的清单
我没有找到答案.
我需要一个包含不同类的列表,每个类都是基类BaseA
固有的,但是每个类都将具有其他类不会拥有的属性,或者某些使用相同类的属性.
I need a List that contains different classes, each of them inherent from a base class BaseA
, but each of them will have properties others will not have, or some that uses the same class will.
public class BaseA
{
public int ID = 0;
}
public class AA : BaseA
{
public int AID = 0;
}
public class AB : BaseA
{
public int BID = 1;
}
public class AC : BaseA
{
public int CID = 0;
}
现在的问题是,我如何获得一个可能包含AA,AB和AC类的列表,而编辑器不会认为我只使用其中的一个.
Now the question is how do I get a single List that may contain class AA,AB and AC and the editor will not think i'm only working with one of them.
我试图制作List<BaseA>
,但是这只会暴露bassA属性,而我需要做的就是能够执行List[0].AID
,如果我说的是AA,它将理解AID的含义.课.
I tried to made the List<BaseA>
, but this will only expose the bassA properties, and what i need is like to be able to do List[0].AID
where it will understand what AID means if i'm talking about the AA class.
我可能全都错了,有人可以指出我正确的方向吗?
I may be going it all wrong, can someone point me to the right direction?
您可以尝试使用 Linq :
List<BaseA> list = ...
var result = list
.OfType<AA>() // Filter out AA instances only
.ElementAt(0) // or just First();
.AID;