5

Let's say I have some basic interface which is generics-driven:

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

Now I have some concrete implementation of this interface which is also generic:

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

This looks OK, but now let's say I have other class:

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

And let's say I want to perform a check if TShouldModelInterface actually implements any of the possible Interface<T>.

If the interface wasn't generic, I would simply write something like where TShouldModelInterface : Interface.

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

3
  • 1
    I modified your title a bit so it is less generic Commented Dec 10, 2010 at 11:31
  • @Daniel Ahahah.. indeed less "generic" Commented Dec 10, 2010 at 11:40
  • In your example, ''InterfaceImpl<T>'' does not implement ''Inteface<T>'' (missing "r", btw). You should declare it as ''public class InterfaceImpl<T> : Inteface<T>'' Commented Dec 10, 2010 at 11:49

1 Answer 1

8
public class Ololo {
   public void BadFunction<TShouldModelInterface, T>(TShouldModelInterface shouldModelInterface)
       where TShouldModelInterface : Interface<T>
   {
      // Whatever
   }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.