【发布时间】:2010-08-30 13:19:56
【问题描述】:
无论如何强制一个泛型定义的约束来实现一个“泛型接口”......也就是说,我希望该类支持传递一个接口和一个约束它的泛型类,以便该类实现该接口。例如,如果我说:
MyGenericClass<IMyInterface, MyImplementation>.DoSomething();
这应该受到约束,以便 MyImplementation 实现 IMyInterface
据我所知可以通过
public class Dynamic_Loader<T, S> where S: T
现在,还有没有强制 T 成为接口?
编辑:这样做的目的是为了:
private static List<T> interfaceList = new List<T>();
public static List<T> InterfaceList {get { return interfaceList;}}
public static void Add(S input) { interfaceList.Add(input);}
并将列表仅限于接口(因为它应该返回某些接口的实现)
【问题讨论】:
标签: c# generics interface constraints