相关疑难解决方法(0)

C#访问泛型类中T类型的静态属性

我试图完成以下场景,通用TestClassWrapper将能够访问它所构成的类的静态属性(它们都将从TestClass派生).就像是:

public class TestClass
{
    public static int x = 5;
}

public class TestClassWrapper<T> where T : TestClass
{
    public int test()
    {
        return T.x;
    }
}
Run Code Online (Sandbox Code Playgroud)

给出错误: 'T' is a 'type parameter', which is not valid in the given context.

有什么建议?

c#

35
推荐指数
3
解决办法
1万
查看次数

标签 统计

c# ×1