C# - static types cannot be used as type arguments

后端 未结 5 1272
北海茫月
北海茫月 2020-11-28 13:57

I\'ve a generic class, that helps me to do checks on argument values

internal sealed class Argument
    where T : class
{
    private void TraceAndT         


        
相关标签:
5条回答
  • 2020-11-28 14:49

    Since static classes cannot be instantiated, it can never create Argument<T> with a static type.

    0 讨论(0)
  • 2020-11-28 14:51

    What you are doing wrong is using a static type as a generic type argument.

    0 讨论(0)
  • 2020-11-28 14:57

    Generics only work with instances, not static classes.

    0 讨论(0)
  • 2020-11-28 15:03

    Since static classes won't have instance members, my concern would be what kind of thing I'm going to do with them.

    I believe that, missing that you can't use static classes as generic arguments, I believe that you need to do this with extension methods instead of a generic class.

    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/extension-methods

    0 讨论(0)
  • 2020-11-28 15:04

    This is deliberate.

    Static classes try to prevent inappropriate use, so in almost all situations, you can't use them in situations where you'd normally want an instance of the type... and that includes type arguments.

    See section 10.1.1.3.1 of the C# 4 spec for the very limited set of situations in which you can refer to static class types.

    0 讨论(0)
提交回复
热议问题