473,387 Members | 1,890 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 1882
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Frank Pleyer via .NET 247 | last post by:
Hi, I got the following problem : I have an defined an Array of different Actions: public PRootActions AllActions = new PRootActions ; I got a dynamic method where all Actions or other...
1
by: Nancy Sui | last post by:
I am trying to dynamically load a dll which specifies different rules for evalution with the following code. DataRow dr = ds.Tables.Rows; string fileName = Convert.ToString(dr); assemblyInstance...
5
by: EdB | last post by:
I have an MDI form with similar child edit forms. The child Edit forms have public methods that of consistent names. I have a tool bar that calls these methods. Here's how I'm doing it now...
2
by: Angel Of Death | last post by:
I have a method. It takes some XML as a parameter. Depending on the content of the XML it should create a specific object and call a KNOWN method. So: public void PersistXml(string XmlData){} ...
8
by: rowe_newsgroups | last post by:
I know this has to be answered in the archives somewhere, but the search results are just confusing me more :-( Anyways, I authored a control library that contains an Interface definition and...
0
by: midhunmathe | last post by:
We have a remote server application called from a client application. The server application is a .NET application but built with COM supportability and included as a COM+ service on one system....
1
by: learning | last post by:
Hi how can I instaltiate a class and call its method. the class has non default constructor. all examples i see only with class of defatul constructor. I am trying to pull the unit test out from...
5
by: =?Utf-8?B?V2F5bmUgQmVycnk=?= | last post by:
I am using reflection and trying to determine if an assembly contains a type that has a particular interface. So my code looks like this: Assembly assembly = Assembly.LoadFile(fileName);...
0
by: rijoshk | last post by:
Hi all; I have a interface which returns another interface, I need to read the inner interface properties at runtime using reflection, how do i go about it? public interface IOuterInterface {...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.