Home All Groups Group Topic Archive Search About

Testing an Assembly to see if it implements a give interface

Author
22 Nov 2004 12:11 AM
Wayne Wise
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

Author
22 Nov 2004 11:43 AM
Nigel Armstrong
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);
    }
}

Show quote
"Wayne Wise" wrote:

> 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
>
Author
23 Nov 2004 2:09 AM
Wayne Wise
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?



Show quote
"Nigel Armstrong" wrote:

> 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" wrote:
>
> > 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
> >
Author
24 Nov 2004 11:45 AM
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


Show quote
"Wayne Wise" <WayneW***@discussions.microsoft.com> escribió en el mensaje
news:C5CFD583-B424-41FF-ABAF-E1027E503E33@microsoft.com...
> 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" wrote:
>
>> 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" wrote:
>>
>> > 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
>> >

AddThis Social Bookmark Button