Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 通用方法,其中T是实现接口的列表_C#_Generics - Fatal编程技术网

C# 通用方法,其中T是实现接口的列表

C# 通用方法,其中T是实现接口的列表,c#,generics,C#,Generics,这类似于 但是,我需要一个通用方法来接受所有实现相同接口的对象列表 此代码给出了一个错误,即没有隐式引用转换 public interface ITest { } public class InterfaceUser : ITest { } public class TestClass { void genericMethod<T>(T myList) where T : List<ITest> { } void testGeneric() {

这类似于

但是,我需要一个通用方法来接受所有实现相同接口的对象列表

此代码给出了一个错误,即没有隐式引用转换

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>());
    }
}
公共接口ITest{}
公共类接口用户:ITest{}
公共类TestClass
{
void genericMethod(T myList),其中T:List{}
void testGeneric()
{
genericMethod(新列表());
}
}

可以这样做吗?

T
定义为
ITest
,并将
列表作为参数

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>());
    }
}
公共接口ITest{}
公共类接口用户:ITest{}
公共类TestClass
{
void genericMethod(List myList),其中T:ITest{}
void testGeneric()
{
this.genericMethod(newlist());
}
}