不允许使用通用接口的通用列表,有其他方法吗?
我正在尝试找到使用通用接口的通用列表作为变量的正确方法。
这是一个例子。它可能不是最好的,但希望您能明白这一点:
public interface IPrimitive<T>
{
T Value { get; }
}
然后在另一个类中,我希望能够声明一个变量,该变量包含实现 IPrimitive
的对象列表任意T
。
// I know this line will not compile because I do not define T
List<IPrimitive<T>> primitives = new List<IPrimitives<T>>;
primitives.Add(new Star()); // Assuming Star implements IPrimitive<X>
primitives.Add(new Sun()); // Assuming Sun implements IPrimitive<Y>
请注意,对于列表中的每个条目,IPrimitive
中的 T
可能不同。
关于如何建立这样的关系有什么想法吗?替代方法?
I am trying to find the right way to use a Generic List of Generic Interfaces as a variable.
Here is an example. It is probably not the best, but hopefully you will get the point:
public interface IPrimitive<T>
{
T Value { get; }
}
and then in another class, I want to be able to declare a variable that holds a list of objects that implement IPrimitive<T>
for arbitrary T
.
// I know this line will not compile because I do not define T
List<IPrimitive<T>> primitives = new List<IPrimitives<T>>;
primitives.Add(new Star()); // Assuming Star implements IPrimitive<X>
primitives.Add(new Sun()); // Assuming Sun implements IPrimitive<Y>
Note that the T
in IPrimitive<T>
could be different for each entry in the list.
Any ideas on how I could setup such a relationship? Alternative Approaches?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你说它不起作用,因为你没有定义
T
。所以定义它:You say it won't work because you don't define
T
. So define it:这是 C# 语言中最复杂的元素之一,尽管它对于构建定义良好的组件非常重要。因此,c# 还不够。然而,这绝对是可能的。
诀窍是由 3 个部分组成:
例如:
这种结构在相互协调类时非常有用,因为您可以指示实现类将使用多个类并进行类型检查验证每个类都遵循既定的类型期望。此外,您可以考虑使用实际的类而不是非泛型类的对象,以便可以对各种非泛型调用的结果执行函数。使用相同的设计,您可以让这些类成为具有自己的实现的通用类,从而创建极其复杂的应用程序。
致OP:请考虑更改已接受的答案,以提高对正确方法的认识,因为之前给出的所有答案都因各种原因而达不到要求,并且可能给读者留下了更多问题。这应该可以处理与集合中的泛型类相关的所有未来问题。
This is one of the most complicated elements of the c# language though it is incredibly important for building well defined components. As such, c# falls short. However it is definitely possible to make this work.
The trick is to have 3 parts:
For example:
This structure is incredibly useful when coordinating classes w/ one another because you're able to indicate that an implementing class will make use of multiple classes and have type checking validate that each class follows established type expectations. In addition, you might consider using an actual class instead of object for the non-generic class so that you can execute functions on the result of the various non-generic calls. Using this same design you can have those classes be generic classes w/ their own implementations and thus create incredibly complex applications.
To OP: Please consider changing the accepted answer to this to raise awareness of the correct approach as all previously stated answers fall short for various reasons and have probably left readers with more questions. This should handle all future questions related to generic classes in a collection.
那么你应该能够拥有
Then you should be able to have
约翰是正确的。
我是否还可以建议(如果您使用的是 C# 4)使您的接口协变?
当您以后需要从列表中删除内容时,这可以为您节省一些麻烦。
John is correct.
Might I also suggest (if you are using C# 4) that you make your interface covariant?
This could save you some trouble later when you need to get things out of the list.