470,626 Members | 2,153 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,626 developers. It's quick & easy.

Reflection: CreateInstance(Type) of class that supports known interface - Get actual interface reference to object?


Hi,

I'm trying to figure out how to dynamically load an assembly and get
an IClientPlugin interface to a class. I can get an "Object"
reference to it after doing an "Activator.CreateInstance" call, but is
there any way to get an IClientPlugin reference to it?

Trying to do a "CType(obj, IClientPlugin)" throws an exception.

Here's the code that loads the assembly and calls a method called
"GetPluginName" that takes no parameters and returns a string:

//////////////////// BEGIN CODE /////////////////////////////

Dim szPluginPath As String

Dim objPluginAssembly As System.Reflection.Assembly
Dim objIClientPlugin As IClientPlugin
Dim obj As Object
Dim objResult As Object
Dim szError As String
Dim T As Type
Dim T2 As Type

szPluginPath = "C:\PLUGINS\\ANET_SystemNews.dll"
objPluginAssembly = System.Reflection.Assembly.LoadFile(szPluginPath)

For Each T In objPluginAssembly.GetTypes()

If T.IsClass() = True Then
'Class implements IClientPlugin interface?
If Not IsNothing(T.GetInterface("IClientPlugin")) Then

'YES, so create an instance of this class
obj = Activator.CreateInstance(T)

' **** objPlugin = CType(obj, IClientPlugin)

'Invoke IClientPlugin.GetPluginName()
objResult = T.InvokeMember("GetPluginName", _
Reflection.BindingFlags.Default Or _
Reflection.BindingFlags.InvokeMethod, _
Nothing, _
obj, _
Nothing)

Debug.WriteLine("Result: {0}", objResult)

End If
End If
Next

//////////////////// END CODE /////////////////////////////

I've commented out the line that causes an error, the CType(obj,
IClientPlugin) call. Here is how the client is implemented:

Public Class AppleNET_BBS_PLUGIN

Implements IClientPlugin

... IClientPlugin functions defined ...

End Class

Is there any way I can get an "IClientPlugin" reference to the
instantiated class? It would make it a lot easier to call methods:

Dim objIClientPlugin As IClientPlugin

.. create class through reflection

objIClientPlugin = ????(obj)

Thanks for any help you can offer!

// CHRIS

Nov 20 '05 #1
10 1796
What exception where you getting?

-Rob Teixeira [MVP]
"Chris Morse" <ch***@sorry.no.spam.com> wrote in message
news:02********************************@4ax.com...
I've commented out the line that causes an error, the CType(obj,
IClientPlugin) call. Here is how the client is implemented:
Is there any way I can get an "IClientPlugin" reference to the
instantiated class?


You should be able to cast to the interface type. Please post exception
message.
Nov 20 '05 #2
On Sat, 10 Jan 2004 21:24:55 -0500, "Rob Teixeira [MVP]"
<RobTeixeira@@msn.com> wrote:
What exception where you getting?

-Rob Teixeira [MVP]


obj = Activator.CreateInstance(T)
Debug.WriteLine(obj.GetType().ToString())

'THIS LINE FAILS:
objPlugin = CType(obj, IClientPlugin)

It throws an "InvalidCastException" with the message
"Specified cast is not valid." But obj is an instance of my class
called "AppleNET_BBS_PLUGIN" which implements IClientPlugin.

obj.GetType().ToString() returns "ANET_SystemNews.AppleNET_BBS_PLUGIN"

Which is the right class and everything:

Public Class AppleNET_BBS_PLUGIN

Implements IClientPlugin

.. functions defined here

End Class
Any ideas? I think I'll create a new set of projects to try and work
this out.. If it doesn't work, maybe someone can take a look at it (?)

Thanks,
// CHRIS
Nov 20 '05 #3
On Sat, 10 Jan 2004 21:24:55 -0500, "Rob Teixeira [MVP]"
<RobTeixeira@@msn.com> wrote:
What exception where you getting?

-Rob Teixeira [MVP]


Say.. I'm wondering if it matters that the IClientPlugin interface I
am trying to use is simply defined in an "Interfaces.vb" file that
both projects use. Besides the "name" of the interface, how can .NET
know that they are really the same interface? Unless it does a
complete type comparison.. Not sure about that..

Is there anything special to do when trying to share interface
definitions across 2 projects?

Thanks,
// CHRIS

Nov 20 '05 #4
Say.. I'm wondering if it matters that the IClientPlugin interface I
am trying to use is simply defined in an "Interfaces.vb" file that
both projects use. Besides the "name" of the interface, how can .NET
know that they are really the same interface? Unless it does a
complete type comparison.. Not sure about that..
Yes it matters a lot. A type's identity is the combination of the
assembly it's in and the full type name. So IClientPlugin in assemblyA
is not the same as IClientPlugin in assemblyB.

Is there anything special to do when trying to share interface
definitions across 2 projects?


Put it in a separate assembly that you reference from both projects.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 20 '05 #5
* Chris Morse <ch***@sorry.no.spam.com> scripsit:
I'm trying to figure out how to dynamically load an assembly and get
an IClientPlugin interface to a class. I can get an "Object"
reference to it after doing an "Activator.CreateInstance" call, but is
there any way to get an IClientPlugin reference to it?

Trying to do a "CType(obj, IClientPlugin)" throws an exception.


Simple sample:

<http://www.mvps.org/dotnet/dotnet/samples/codingtechnique/downloads/PlugIns.zip>

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
Nov 20 '05 #6
On Sun, 11 Jan 2004 10:55:42 +0100, Mattias Sjögren
<ma********************@mvps.org> wrote:
Say.. I'm wondering if it matters that the IClientPlugin interface I
am trying to use is simply defined in an "Interfaces.vb" file that
both projects use. Besides the "name" of the interface, how can .NET
know that they are really the same interface? Unless it does a
complete type comparison.. Not sure about that..


Yes it matters a lot. A type's identity is the combination of the
assembly it's in and the full type name. So IClientPlugin in assemblyA
is not the same as IClientPlugin in assemblyB.

Is there anything special to do when trying to share interface
definitions across 2 projects?


Put it in a separate assembly that you reference from both projects.

Mattias


Thanks for the help everyone!

Creating a new "Class Library" project, putting the interfaces I want
in there, and then referencing them from each project works great.

Cheers!
// CHRIS

Nov 20 '05 #7
Chris,
Thanks for the help everyone!

Creating a new "Class Library" project, putting the interfaces I want
in there, and then referencing them from each project works great. I find its better to reference the project (as it sounds like you did) in
the solution rather then attempting to reference the compiled DLL.

As invariable the compiled DLL is created in the incorrect order and the
AssemblyVersion attribute does not match, causing the "same" problem.

Remember the assembly version is part of the assembly name used to match the
type, allowing for side by side of newer versions of the assemblies...

For details on the "Fully Qualified Type Names" see:

http://msdn.microsoft.com/library/de...dTypeNames.asp

There was an MSDN Magazine article also, but I'm not finding it just now.

Hope this helps
Jay

"Chris Morse" <ch***@sorry.no.spam.com> wrote in message
news:b7********************************@4ax.com... On Sun, 11 Jan 2004 10:55:42 +0100, Mattias Sjögren
<ma********************@mvps.org> wrote:
Say.. I'm wondering if it matters that the IClientPlugin interface I
am trying to use is simply defined in an "Interfaces.vb" file that
both projects use. Besides the "name" of the interface, how can .NET
know that they are really the same interface? Unless it does a
complete type comparison.. Not sure about that..


Yes it matters a lot. A type's identity is the combination of the
assembly it's in and the full type name. So IClientPlugin in assemblyA
is not the same as IClientPlugin in assemblyB.

Is there anything special to do when trying to share interface
definitions across 2 projects?


Put it in a separate assembly that you reference from both projects.

Mattias


Thanks for the help everyone!

Creating a new "Class Library" project, putting the interfaces I want
in there, and then referencing them from each project works great.

Cheers!
// CHRIS

Nov 20 '05 #8
On Sun, 11 Jan 2004 12:40:15 -0600, "Jay B. Harlow [MVP - Outlook]"
<Ja************@msn.com> wrote:
I find its better to reference the project (as it sounds like you did) in
the solution rather then attempting to reference the compiled DLL.

As invariable the compiled DLL is created in the incorrect order and the
AssemblyVersion attribute does not match, causing the "same" problem.


I'm not sure how to do this. When I click the "References" in the
solution explorer and select "Add Reference" and then selected the
"Projects" tab in the dialog, I can only reference actual DLL files.

So I am just referencing the DLL file in the project's obj\Release
folder. Is there a way to reference the project itself or something?

thanks,
// CHRIS

Nov 20 '05 #9
* Chris Morse <ch***@sorry.no.spam.com> scripsit:
So I am just referencing the DLL file in the project's obj\Release
folder. Is there a way to reference the project itself or something?


Have a look at the "Project dependencies..." menu item in the project's
context menu.

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
Nov 20 '05 #10
On 15 Jan 2004 21:05:33 +0100, hi***************@gmx.at (Herfried K.
Wagner [MVP]) wrote:
* Chris Morse <ch***@sorry.no.spam.com> scripsit:
So I am just referencing the DLL file in the project's obj\Release
folder. Is there a way to reference the project itself or something?


Have a look at the "Project dependencies..." menu item in the project's
context menu.


Doh! Thanks!

// CHRIS

Nov 20 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Frank Pleyer via .NET 247 | last post: by
5 posts views Thread by EdB | last post: by
8 posts views Thread by rowe_newsgroups | last post: by
1 post views Thread by learning | last post: by
5 posts views Thread by =?Utf-8?B?V2F5bmUgQmVycnk=?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.