Question about C # generators - common interface limitation

Say I have a basic interface managed by generics:

public interface Inteface<T> {
   void Foo(T t);
}

Now I have a specific implementation of this interface, which is also common:

public class InterfaceImpl<T> {
   public void Foo(T t) {
      // Whatever
   }
}

This looks fine, but now let's say I have a different class:

public class Ololo {
   public void BadFunction<TShouldModelInterface>(TShouldModelInterface shouldModelInterface) {
      // Whatever
   }
}

And let me say that I want to perform a check if it TShouldModelInterfaceactually implements any of the possible ones Interface<T>.

If the interface was not shared, I would just write something like where TShouldModelInterface : Interface.

But is there a way to solve this problem if the interface is declared as Interface<T>?

+5
source share
1 answer
public class Ololo {
   public void BadFunction<TShouldModelInterface, T>(TShouldModelInterface shouldModelInterface)
       where TShouldModelInterface : Interface<T>
   {
      // Whatever
   }
}
+8
source

All Articles