Testing an Assembly to see if it implements a give interface

G

Guest

I have an application that gets all the assemblies in a folder structure.
Once I have that list I want to test each assembly to see if it implements a
interface I require. This is what I currently have using C#:

Assembly a = Assembly.LoadFile( file );
Type test = a.GetType( "InterfaceTest.IMyInterface" );

if( test != null )
{
// Valid Assembly for use
// ... do stuff
}

Is there a better to do this? I tried using the types of the assembly and
comparing them to IMyInterface but could not get that to work. I would
really like to do this step with out the type test like above if possible.
 
G

Guest

Hi Wayne

Perhaps this would be better in a different group...but here's one approach:

HTH

Nigel Armstrong

System.Reflection.Assembly a =
System.Reflection.Assembly.LoadFile(@"C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\System.Windows.Forms.dll");
System.Type[] types = a.GetTypes();

foreach (Type t in types)
{
if (t.GetInterface("System.IDisposable") != null)
{
Console.WriteLine("{0} supports interface System.IDisposable", t.Name);
}
}
 
G

Guest

Nigel,
What would be the appropriate group to post the message in? Thank you for
you reply, but I was looking for something more like code I have seen for
generics where I could potential say something like

if( a.GetType("InterfaceTest.IMyInterface" ) is IMyInterface )

or


foreach (Type t in types)
{
if ( t.GetInterface("System.IDisposable") == typeof( System.IDisposable ) )
{
Console.WriteLine("{0} supports interface System.IDisposable", t.Name);
}
}

What I’m trying to accomplish is to ensure whatever comparison method I use
validates against the interface in my project. I want to make sure that it
is my InterfaceTest.IMyInterface exactly that is matched and not another
InterfaceTest.IMyInterface. I think with all the methods I have seen so far
some else could a similar interface and I would mistake it for the one I
expect. That is not very likely but possible. I believe this is possible
unless the interface someone else wrote matched my exactly then the
signatures would be the same regardless?



Nigel Armstrong said:
Hi Wayne

Perhaps this would be better in a different group...but here's one approach:

HTH

Nigel Armstrong

System.Reflection.Assembly a =
System.Reflection.Assembly.LoadFile(@"C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\System.Windows.Forms.dll");
System.Type[] types = a.GetTypes();

foreach (Type t in types)
{
if (t.GetInterface("System.IDisposable") != null)
{
Console.WriteLine("{0} supports interface System.IDisposable", t.Name);
}
}

Wayne Wise said:
I have an application that gets all the assemblies in a folder structure.
Once I have that list I want to test each assembly to see if it implements a
interface I require. This is what I currently have using C#:

Assembly a = Assembly.LoadFile( file );
Type test = a.GetType( "InterfaceTest.IMyInterface" );

if( test != null )
{
// Valid Assembly for use
// ... do stuff
}

Is there a better to do this? I tried using the types of the assembly and
comparing them to IMyInterface but could not get that to work. I would
really like to do this step with out the type test like above if possible.

--
Wayne R. Wise

"Life shrinks or expands in proportion to one's courage. "
Anais Nin, The Diary of Anais Nin, volume 3, 1939-1944
 
C

Carlos J. Quintero [MVP]

Hi,

A more appropiate group would be one not related to windows forms, such as
microsoft.public.dotnet.framework.

Regarding to your question, you can compare full type names:

mytypeFullName = GetType(MyType).FullName

for each objType ...
if (objType.FullName == mytypeFullName)
...

A last note: you may prefer to use Assembly.GetExportedTypes() instead of
Assembly.GetTypes since it is much faster because you are not interested in
non public types...

--

Carlos J. Quintero

The MZ-Tools all-in-one add-in, now for .NET: http://www.mztools.com


Wayne Wise said:
Nigel,
What would be the appropriate group to post the message in? Thank you for
you reply, but I was looking for something more like code I have seen for
generics where I could potential say something like

if( a.GetType("InterfaceTest.IMyInterface" ) is IMyInterface )

or


foreach (Type t in types)
{
if ( t.GetInterface("System.IDisposable") == typeof(
System.IDisposable ) )
{
Console.WriteLine("{0} supports interface System.IDisposable", t.Name);
}
}

What I'm trying to accomplish is to ensure whatever comparison method I
use
validates against the interface in my project. I want to make sure that
it
is my InterfaceTest.IMyInterface exactly that is matched and not another
InterfaceTest.IMyInterface. I think with all the methods I have seen so
far
some else could a similar interface and I would mistake it for the one I
expect. That is not very likely but possible. I believe this is possible
unless the interface someone else wrote matched my exactly then the
signatures would be the same regardless?



Nigel Armstrong said:
Hi Wayne

Perhaps this would be better in a different group...but here's one
approach:

HTH

Nigel Armstrong

System.Reflection.Assembly a =
System.Reflection.Assembly.LoadFile(@"C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\System.Windows.Forms.dll");
System.Type[] types = a.GetTypes();

foreach (Type t in types)
{
if (t.GetInterface("System.IDisposable") != null)
{
Console.WriteLine("{0} supports interface System.IDisposable", t.Name);
}
}

Wayne Wise said:
I have an application that gets all the assemblies in a folder
structure.
Once I have that list I want to test each assembly to see if it
implements a
interface I require. This is what I currently have using C#:

Assembly a = Assembly.LoadFile( file );
Type test = a.GetType( "InterfaceTest.IMyInterface" );

if( test != null )
{
// Valid Assembly for use
// ... do stuff
}

Is there a better to do this? I tried using the types of the assembly
and
comparing them to IMyInterface but could not get that to work. I would
really like to do this step with out the type test like above if
possible.

--
Wayne R. Wise

"Life shrinks or expands in proportion to one's courage. "
Anais Nin, The Diary of Anais Nin, volume 3, 1939-1944
 

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