Gert Hermans
Gert Hermans

Reputation: 829

Generic passing of interface and class implementing interface

I'm trying to create a method that through generics tries to pass an interface and a class. It should look something like this, but this does not build:

public static void Register<TInterface, TClass>() 
  where TClass     : TInterface 
  where TInterface : interface // <- doesn't compile
{
   ...
}

Is there any way to get this to work. The idea is to build a wrapper around an ioc container.

Upvotes: 0

Views: 76

Answers (1)

kkica
kkica

Reputation: 4104

You cannot do it the way you want. The closest you will be able to achieve is by limiting TClass to inherit/implement the TInterface, but TInterface can also be a class (and TClass can also be an interface. Perhaps its worth noting that class does not mean class but any reference type. It can also be an interface)

public static void Register<TInterface, TClass>() 
  where TClass     : class, TInterface
  where TInterface : class
{

}

Upvotes: 1

Related Questions