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

  • Thread starter Thread starter Voivod
  • Start date Start date
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);
 
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.
 
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
}
 
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
 
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
 
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.
 
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?
 
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
}
}
}
 
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
 
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.
 
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 :)
 
Back
Top