【发布时间】:2012-10-27 23:55:11
【问题描述】:
我想创建一个类型列表,每个类型都必须实现一个特定的接口。喜欢:
interface IBase { }
interface IDerived1 : IBase { }
interface IDerived2 : IBase { }
class HasATypeList
{
List<typeof(IBase)> items;
HasATypeList()
{
items.Add(typeof(IDerived1));
}
}
所以我知道我可以做到
List<Type> items;
但这不会将列表中允许的类型限制为实现 IBase 的类型。我必须编写自己的列表类吗?不是说这有什么大不了,但如果我不必……
【问题讨论】:
-
我以为我有答案,但你是对的——你不想要一个 IBase 实现对象的列表,你想要一个实现该接口的 TYPE 列表。我认为您必须在 Add 中使用额外的逻辑来实现自己的列表
标签: c# reflection collections