且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在C#中将泛型作为泛型类型参数传递

更新时间:2021-11-09 23:42:05

您可以将第二个通用参数添加到实现类中.下面的静态 Example 方法显示了此示例.

You can add the second generic parameter to the implementation class. The static Example method below shows an example of this.

public interface ITest<T>
{
    T GetValue();
}

public class Test<T, U> where T : ITest<U>
{
    public U GetValue(T input)
    {
        return input.GetValue();
    }
}

public class Impl : ITest<string>
{
    public string GetValue()
    {
        return "yay!";
    }

    public static void Example()
    {
        Test<Impl, string> val = new Test<Impl,string>();
        string result = val.GetValue(new Impl());
    }
}