[Reflection] best way to know if a Type implements an interface

V

Voivod

Suppose I have an interface IPlugin, and a Type myType.

What's the best way to ask myType if it implements the IPlugin
interface?

I came out with this, but I don't know if there are better
alternatives:
bool implementsIPlugin = typeof(IPlugin).IsAssignableFrom(myType);
 
J

Jon Skeet [C# MVP]

Voivod said:
Suppose I have an interface IPlugin, and a Type myType.

What's the best way to ask myType if it implements the IPlugin
interface?

I came out with this, but I don't know if there are better
alternatives:
bool implementsIPlugin = typeof(IPlugin).IsAssignableFrom(myType);

Seems like a very good way to me. I can't remember off-hand whether it
will return true if myType is an interface deriving from IPlugin though
- one to check.
 
T

Tigger

Seems like a very good way to me. I can't remember off-hand whether it
will return true if myType is an interface deriving from IPlugin though
- one to check.


if (myType is IPlugin)
{
IPlugin myPlugin = (IPlugin)myType;
// do stuff
}

// or

IPlugin myPlugin = myType as IPlugin;

if (myPlugin!=null)
{
// do stuff
}
 
M

Moty Michaely

if (myType is IPlugin)
{
IPlugin myPlugin = (IPlugin)myType;
// do stuff

}

// or

IPlugin myPlugin = myType as IPlugin;

if (myPlugin!=null)
{
// do stuff

}

Tiger,

I it will not work since myType is not an instance of the object, it's
an instance of the Type class. (Voivod, correct me if I'm wrong.)

This would be a good solution in case of an IPlugin derived instance.

Moty
 
M

Moty Michaely

Seems like a very good way to me. I can't remember off-hand whether it
will return true if myType is an interface deriving from IPlugin though
- one to check.

I think it will work:

c - The Type to compare with the current Type.

IsAssignableFrom returns:
"true if c and the current Type represent the same type, or if the
current Type is in the inheritance hierarchy of c, or if the current
Type is an interface that c implements, or if c is a generic type
parameter and the current Type represents one of the constraints of c.
false if none of these conditions are true, or if c is a null
reference (Nothing in Visual Basic). "

Moty
 
J

Jon Skeet [C# MVP]

Moty Michaely said:
I think it will work:

c - The Type to compare with the current Type.

IsAssignableFrom returns:
"true if c and the current Type represent the same type, or if the
current Type is in the inheritance hierarchy of c, or if the current
Type is an interface that c implements, or if c is a generic type
parameter and the current Type represents one of the constraints of c.
false if none of these conditions are true, or if c is a null
reference (Nothing in Visual Basic). "

Yes - that *sounds* like it will return true for an interface deriving
from the plugin interface, which isn't what the OP wants, I think - you
can't create an instance of it, basically.
 
M

Moty Michaely

Yes - that *sounds* like it will return true for an interface deriving
from the plugin interface, which isn't what the OP wants, I think - you
can't create an instance of it, basically.

What doe's the OP wants?
 
T

Tigger

Tiger,

I it will not work since myType is not an instance of the object, it's
an instance of the Type class. (Voivod, correct me if I'm wrong.)

This would be a good solution in case of an IPlugin derived instance.

Moty


Sorry, didn't read the question properly.

The other suggestion works.

You might want to add some other checks if you want to make sure it
"implements" e.g. this is how I find pluggins from an assembly:

Assembly asm = AppDomain.CurrentDomain.Load(path);
foreach(Type type in asm.GetTypes())
{
if (type.IsClass && !type.IsAbstract)
{
if (typeof(IPlugin).IsAssignableFrom(type))
{
// type is a pluggin
}
}
}
 
M

Moty Michaely

Sorry, didn't read the question properly.

The other suggestion works.

You might want to add some other checks if you want to make sure it
"implements" e.g. this is how I find pluggins from an assembly:

Assembly asm = AppDomain.CurrentDomain.Load(path);
foreach(Type type in asm.GetTypes())
{
if (type.IsClass && !type.IsAbstract)
{
if (typeof(IPlugin).IsAssignableFrom(type))
{
// type is a pluggin
}
}

}

Hi,

That's okay =).

Have a great day,
Moty
 
J

Jon Skeet [C# MVP]

What doe's the OP wants?

Given the example, I expect he wants to find types to instantiate which
implement the plugin interface. This wouldn't include other interfaces
- although it also wouldn't include abstract types, so he'll need an
extra check there anyway.

Admittedly I'm inferring the purpose from the original post, but I
don't think it's much of a leap.
 
V

Voivod

Given the example, I expect he wants to find types to instantiate which
implement the plugin interface. This wouldn't include other interfaces
- although it also wouldn't include abstract types, so he'll need an
extra check there anyway.

I'm the OP, under another account.
Yes, you're right... Thanks to all for your precious answers :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top