RSS

Get Types That Inherit Certain Interface in Reflection

15 Jun

First approach:

Assembly.GetTypes().Where(t => t.GetInterfaces().Contains(typeof (ICustomer)));

This approach is sort of workaround by getting all the interfaces that the Type (t) inherits and loop thorough them.

Source: MSDN

 

Second approach (preferred):

Assembly.GetTypes().Where(t => (typeof(ICustomer)).IsAssignableFrom(t));

Type.IsAssignableFrom is method to determine whether the current Type (t) can be assigned from an instance of the specified Type (ICustomer). The specified Type doesn’t have to be an interface.

Source: MSDN

Advertisements
 
Leave a comment

Posted by on June 15, 2012 in General

 

Tags: , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

 
%d bloggers like this: