[英]Generic Method where T is List that implements interface
This is similar to C# - Multiple generic types in one list这类似于C# - 一个列表中的多个泛型类型
However, I want a generic method to accept a List of objects that all implement the same interface.但是,我想要一个通用方法来接受所有实现相同接口的对象列表。
This code gives the error that there is no implicit reference conversion.此代码给出了没有隐式引用转换的错误。
public interface ITest { }
public class InterfaceUser : ITest { }
public class TestClass
{
void genericMethod<T>(T myList) where T : List<ITest> { }
void testGeneric()
{
genericMethod(new List<InterfaceUser>());
}
}
Can this be done?这可以做到吗?
Define T
as ITest
and take a List<T>
as argument将
T
定义为ITest
并将List<T>
作为参数
public interface ITest { }
public class InterfaceUser : ITest { }
public class TestClass
{
void genericMethod<T>(List<T> myList) where T : ITest { }
void testGeneric()
{
this.genericMethod(new List<InterfaceUser>());
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.