Search code examples
c#genericsgeneric-programming

Is possible to point that the type used for a generic method, should be an interface?


Here is my generic method code:

  public static IT Activate<IT>(string path)
  {
        //some code here....
  }

I'd want to set that generic IT must be only an interface.

Is this possible?


Solution

  • No, there's no such constraint in C#, or in .NET generics in general. You'd have to check at execution time.

    if (!typeof(IT).IsInterface)
    {
        // Presumably throw an exception
    }