Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Assistance with Generics

Status
Not open for further replies.

jmeckley

Programmer
Jul 15, 2002
5,269
US
This is escaping me, and I don't work with metadata enough to connect the dots. here is the setup
Code:
interface IListener<T>
{
  void Handle(T message);
}

class Presenter 
   : BaseClass
   , IAnotherInterface
   , IListener<This>
   , IListener<That>
{
   public void Handle(This message)
   {
   }
   public void Handle(That message)
   {
   }
}
based on the type, how can I tell it implements IListener? It doesn't matter what the closed type is, only that it's implemented. for example:
Code:
var listener = typeof(IListener<>);
var presenter = typeof(Presenter);

var implementsListener = presenter
   .GetInterfaces()
   .Any(interface => interface ???? listener);

//implementsListener is True
thank in advance.

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
some trial and error and i figured it out.
Code:
var isAListener = typeof(Presenter)
   .GetInterfaces()
   .Where(i => i.IsGenericType)
   .Select(i => i.GetGenericTypeDefinition())
   .Any(i => i == listener);

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top