Determining if a type implements a particular interface

M

Mario Vargas

Hello all,

When using reflection, how can I find out if a particular type implements a
specific interface. For instance, I have the following code:

Type t = myAsm.GetType( "AssemblyToLoad.MyClass" );
Type iMyClassType = typeof(AssemblyToLoad.IMyClass);
foreach( Type i in t.GetInterfaces() )
{
Console.WriteLine( "Interface: {0}; Implemented: {1}, {2}",
i,
iMyClassType,
i.Equals( iMyClassType ) );
}

The type "AssemblyToLoad.MyClass" implements an interface called
"AssemblyToLoad.IMyClass", but the test "i.Equals( iMyClassType )" always
returns false. The code above is in a different assembly from the
"AssemblyToLoad" namespace.

Thanks for your help and tips...

Mario
 
M

Mattias Sjögren

When using reflection, how can I find out if a particular type implements a
specific interface. For instance, I have the following code:

Type t = myAsm.GetType( "AssemblyToLoad.MyClass" );
Type iMyClassType = typeof(AssemblyToLoad.IMyClass);

Why are you using Assembly.GetType for the class and typeof for the
interface if they are both defined in the same assembly?

Is there any chance you have two separate definitions of the IMyClass
interface?

BTW I like to use Type.IsAssignableFrom rather than looping through
all interfaces.


Mattias
 
M

Mario Vargas

The class AssemblyToLoad.MyClass and interface AssemblyToLoad.IMyclass are
defined in an assembly different from my client application.

I am emperimenting with loading a class defined in a separate assembly from
my client application that shares a common interface type. In my example,
that common interface is IMyClass. What I would like to do is create a
system where different implementations of an interface I am desigining can
be loaded at runtime using reflection or any other means. Reflection is a
new field for me.

I am also trying to use the Activator.CreateInstanceFrom( string, string )
method to accomplish my goal in a much more concise manner, but I get an
InvalidCastException. I am duplicating the example code shown in the
documentation for the Activator.CreateInstanceFrom(string, string, object[])
overloaded method.

I don't think I have two separate definitions of the same interface. I'll
check again.

I'll look at the documentation for Type.IsAssignableFrom. Thanks so much for
sharing that tip.

Mario
 
U

UL-Tomten

loading a class defined in a separate assembly [...] create a system where
different implementations of an interface I am desigining can be loaded
at runtime using reflection [...] use the Activator.CreateInstanceFrom([...])
[...] InvalidCastException.

Just to reassure you: this is a pretty standard way to do it. It's
sometimes called the object builder pattern, I think. If you get
casting errors, make sure your references are correct, and that you
have no redundant DLLs, or old DLLs lying around, accidentally being
used. If you load an existing assembly from disk, make sure you
specify the correct assembly details.

This is a typical scenario with three assemblies, from the top of my
head:

1. ObjectBuilder assembly references assembly which contains IClass
and assembly which contains Class
2. Client assembly references assembly which contains IClass and
assembly which contains IClass
3. Client does IClass classInstance = ObjectBuilder<IClass>.Build(),
upon which ObjectBuilder uses reflection to CreateInstance() of Class,
casting it as IClass, and returning it.

I've used a shortcut from System.Web.dll sometimes:

http://msdn2.microsoft.com/en-us/library/system.web.compilation.buildmanager_methods.aspx

It does iteration over assemblies and types to find a specified type,
which you might end up doing yourself otherwise, if you want a real
System.Type.
 
U

UL-Tomten

Not in this case, where iMyClassType is a System.Type instance.

Also, the correct form is "i am", not "i is". The new Code Analysis in
Visual Studio 2008 would generate a OperatorShouldBeVerbedCorrectly
warning for this error.
 
G

Guest

Since when has "am" been a keyword in C#/.Net ?
I think you've misread the code slightly. Something more readable might be :

public void DoSomething(object obj)
{
if( obj is iMyClassType )
{
}
}

HTH

Ged



Not in this case, where iMyClassType is a System.Type instance.

Also, the correct form is "i am", not "i is". The new Code Analysis in
Visual Studio 2008 would generate a OperatorShouldBeVerbedCorrectly
warning for this error.
 
M

Mario Vargas

Thank you all for your contributions!


UL-Tomten said:
loading a class defined in a separate assembly [...] create a system
where
different implementations of an interface I am desigining can be loaded
at runtime using reflection [...] use the
Activator.CreateInstanceFrom([...])
[...] InvalidCastException.

Just to reassure you: this is a pretty standard way to do it. It's
sometimes called the object builder pattern, I think. If you get
casting errors, make sure your references are correct, and that you
have no redundant DLLs, or old DLLs lying around, accidentally being
used. If you load an existing assembly from disk, make sure you
specify the correct assembly details.

This is a typical scenario with three assemblies, from the top of my
head:

1. ObjectBuilder assembly references assembly which contains IClass
and assembly which contains Class
2. Client assembly references assembly which contains IClass and
assembly which contains IClass
3. Client does IClass classInstance = ObjectBuilder<IClass>.Build(),
upon which ObjectBuilder uses reflection to CreateInstance() of Class,
casting it as IClass, and returning it.

I've used a shortcut from System.Web.dll sometimes:

http://msdn2.microsoft.com/en-us/library/system.web.compilation.buildmanager_methods.aspx

It does iteration over assemblies and types to find a specified type,
which you might end up doing yourself otherwise, if you want a real
System.Type.
 

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