C# accessing a static property of type T in a generic class

前端 未结 7 1528
后悔当初
后悔当初 2020-12-16 09:30

I am trying to accomplish the following scenario that the generic TestClassWrapper will be able to access static properties of classes it is made of (they will all derive fr

7条回答
  •  忘掉有多难
    2020-12-16 10:00

    Generics do not support anything related to static members, so that won't work. My advice would be: don't make it static. Assuming the field genuinely relates to the specific T, you could also use reflection:

    return (int) typeof(T).GetField("x").GetValue(null);
    

    but I don't recommend it.

提交回复
热议问题