|
|
|
start date: Mon, 13 Aug 2007 12:49:49 -0400,
posted on: microsoft.public.dotnet.framework
back
| Thread Index |
|
1
Mario Vargas
|
|
2
Mattias Sjgren
|
|
3
Mario Vargas
|
|
4
UL-Tomten
|
|
5
Kevin Spencer
|
|
6
Mattias Sjgren
|
|
7
UL-Tomten
|
|
8
Ged
|
|
9
Mario Vargas
|
Determining if a type implements a particular interface
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
Date:Mon, 13 Aug 2007 12:49:49 -0400
Author:
|
Re: Determining if a type implements a particular interface
>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
--
Mattias Sjgren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Date:Mon, 13 Aug 2007 20:25:59 +0200
Author:
|
Re: Determining if a type implements a particular interface
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
"Mattias Sjgren" wrote in message
news:%23Vu85dd3HHA.5316@TK2MSFTNGP04.phx.gbl...
> >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
>
> --
> Mattias Sjgren [C# MVP] mattias @ mvps.org
> http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
> Please reply only to the newsgroup.
Date:Mon, 13 Aug 2007 14:53:44 -0400
Author:
|
Re: Determining if a type implements a particular interface
On Aug 13, 8:53 pm, "Mario Vargas" wrote:
> 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.
Date:Mon, 13 Aug 2007 14:32:57 -0700
Author:
|
Re: Determining if a type implements a particular interface
Use the "is" operator:
if (i is iMyClassType) ...
--
HTH,
Kevin Spencer
Microsoft MVP
DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net
"Mario Vargas" wrote in message
news:uurx4nc3HHA.5424@TK2MSFTNGP02.phx.gbl...
> 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
>
Date:Tue, 14 Aug 2007 07:50:14 -0400
Author:
|
Re: Determining if a type implements a particular interface
>Use the "is" operator:
>
>if (i is iMyClassType) ...
Not in this case, where iMyClassType is a System.Type instance.
Mattias
--
Mattias Sjgren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Date:Tue, 14 Aug 2007 16:10:38 +0200
Author:
|
Re: Determining if a type implements a particular interface
On Aug 14, 4:10 pm, Mattias Sjögren
wrote:
>> if (i is iMyClassType) ...
>
> 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.
Date:Tue, 14 Aug 2007 07:31:53 -0700
Author:
|
Re: Determining if a type implements a particular interface
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
"UL-Tomten" wrote in message
news:1187101913.902323.197560@19g2000hsx.googlegroups.com...
On Aug 14, 4:10 pm, Mattias Sjgren
wrote:
>> if (i is iMyClassType) ...
>
> 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.
Date:Tue, 14 Aug 2007 15:49:43 +0100
Author:
|
Re: Determining if a type implements a particular interface
Thank you all for your contributions!
"UL-Tomten" wrote in message
news:1187040777.027872.202230@d55g2000hsg.googlegroups.com...
>
> On Aug 13, 8:53 pm, "Mario Vargas" wrote:
>
>> 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.
>
Date:Tue, 14 Aug 2007 15:32:57 -0400
Author:
|
|
|