473,466 Members | 1,313 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Casting an object as an Interface that it implements

Ok, I've googled long and hard, but I can't find anything relevant on this
one. I am now at the mercy of your good nature. <g>

I have 3 projects in my solution: One app and 2 libraries.

I am basically attempting to create a "plug-in" feature for my app.

Namespace A contains the plug-in interface
Namespace B is a sample implementation of that interface.
The application needs to be able to load the implementation in Namespace B
and use it via the interface.

Line 10 is where I create an instance of class BWInventory
Line 20 is where I get the following error when I try to get a reference to
the Interface

"An unhandled exception of type 'System.InvalidCastException' occurred in
BookWorks.exe
Additional Information: Specified cast is not valid"

I can continue to use it as an "object" and I can even call the interface
methods, but for various reasons, I need to have a real reference to the
interface.
Help! Thanks!
Namespace A ' DLL. referenced by App and B
Public Interface IBWModule
ReadOnly Property Name() As String
End Interface
End Namespace

Namespace B 'Loaded dynamically by App
Public Class BWInventory
Implements IBWModule

Public ReadOnly Property Name() As String Implements
BookWorksCommon.IBWModule.Name
Get
Return "BookWorks Inventory"
End Get
End Property
End Class
End Namespace

Namespace App 'References A and dynamically loads B.
Imports BookWorksCommon
Public Class frmMain
Inherits System.Windows.Forms.Form
Dim WithEvents mfrmLogin As frmLogin
Dim colModules As Collection
Dim intCurrentModuleIndex As Integer

Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim PluginAssembly As System.Reflection.Assembly
Dim CommonAssembly As System.Reflection.Assembly
Dim t As Type
Dim i As Type
Dim x As Object 'IBWModule
Dim w As IBWModule
Dim typModule As Type

PluginAssembly =
System.Reflection.Assembly.LoadFrom("C:\Documents and Settings\mjozwik_2\My
Documents\Visual Studio
Projects\BookWorks\BookWorksInventory\bin\BookWork sInventory.dll")
CommonAssembly =
System.Reflection.Assembly.LoadFrom("C:\Documents and Settings\mjozwik_2\My
Documents\Visual Studio
Projects\BookWorks\BookWorksCommon\bin\BookWorksCo mmon.dll")

typModule = CommonAssembly.GetType("BookWorksCommon.IBWModule" )
colModules = New Collection

For Each t In PluginAssembly.GetTypes
For Each i In t.GetInterfaces
If i.Equals(typModule) Then
10 x = Activator.CreateInstance(t)
colModules.Add(x)
20 w = CType(x,IBWModule)
End If
Next
Next
End Sub
End Class
End Namespace
Nov 20 '05 #1
3 1844
Nak
Hi there Jeff,

You might want to take a look at this...

http://www.planet-source-code.com/vb...1495&lngWId=10

It's my example of how to make a plugin engine. I'm using practically
the same code in an application that I am using at the moment and it works
fine, I have just added features. Hopefully this will show you how to
achieve what you want.

Nick.

--
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
"No matter. Whatever the outcome, you are changed."

Fergus - September 5th 2003
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
"Jeff Molby" <JeffMolby@C_mc_st.n_t> wrote in message
news:uX**************@TK2MSFTNGP11.phx.gbl...
Ok, I've googled long and hard, but I can't find anything relevant on this
one. I am now at the mercy of your good nature. <g>

I have 3 projects in my solution: One app and 2 libraries.

I am basically attempting to create a "plug-in" feature for my app.

Namespace A contains the plug-in interface
Namespace B is a sample implementation of that interface.
The application needs to be able to load the implementation in Namespace B
and use it via the interface.

Line 10 is where I create an instance of class BWInventory
Line 20 is where I get the following error when I try to get a reference to the Interface

"An unhandled exception of type 'System.InvalidCastException' occurred in
BookWorks.exe
Additional Information: Specified cast is not valid"

I can continue to use it as an "object" and I can even call the interface
methods, but for various reasons, I need to have a real reference to the
interface.
Help! Thanks!
Namespace A ' DLL. referenced by App and B
Public Interface IBWModule
ReadOnly Property Name() As String
End Interface
End Namespace

Namespace B 'Loaded dynamically by App
Public Class BWInventory
Implements IBWModule

Public ReadOnly Property Name() As String Implements
BookWorksCommon.IBWModule.Name
Get
Return "BookWorks Inventory"
End Get
End Property
End Class
End Namespace

Namespace App 'References A and dynamically loads B.
Imports BookWorksCommon
Public Class frmMain
Inherits System.Windows.Forms.Form
Dim WithEvents mfrmLogin As frmLogin
Dim colModules As Collection
Dim intCurrentModuleIndex As Integer

Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim PluginAssembly As System.Reflection.Assembly
Dim CommonAssembly As System.Reflection.Assembly
Dim t As Type
Dim i As Type
Dim x As Object 'IBWModule
Dim w As IBWModule
Dim typModule As Type

PluginAssembly =
System.Reflection.Assembly.LoadFrom("C:\Documents and Settings\mjozwik_2\My Documents\Visual Studio
Projects\BookWorks\BookWorksInventory\bin\BookWork sInventory.dll")
CommonAssembly =
System.Reflection.Assembly.LoadFrom("C:\Documents and Settings\mjozwik_2\My Documents\Visual Studio
Projects\BookWorks\BookWorksCommon\bin\BookWorksCo mmon.dll")

typModule = CommonAssembly.GetType("BookWorksCommon.IBWModule" ) colModules = New Collection

For Each t In PluginAssembly.GetTypes
For Each i In t.GetInterfaces
If i.Equals(typModule) Then
10 x = Activator.CreateInstance(t)
colModules.Add(x)
20 w = CType(x,IBWModule)
End If
Next
Next
End Sub
End Class
End Namespace

Nov 20 '05 #2
I just found it. Somewhat of a variation on what you described in your site.

Because the IDE doesn't copy the DLL to my main app's bin directory (there's
no static reference), I was just loading the DLL from its own bin directory.
However, That directory had its own copy of the Interface assembly, so it
behaved just as you described in your site.

Sorry about the stupid mistake and thank you for your help!

"Jon Skeet" <sk***@pobox.com> wrote in message
news:MP************************@news.microsoft.com ...
Jeff Molby <JeffMolby@C_mc_st.n_t> wrote:
First off, this is a VB application, but I know you C# are used to working at the lower levels like this, so I'm hoping this is an easy one for you. Then you can continue to feel superior to us VB-types <G> BTW, I am somewhat proficient in C#, but my bosses and coworkers barely know VB, so no, I can't just rewrite it in C# <g>

Ok, I've googled long and hard, but I can't find anything relevant on this one. I am now at the mercy of your good nature. <g>

I have 3 projects in my solution: One app and 2 libraries.


I suspect the problem you've got is the one I've described here:
http://www.pobox.com/~skeet/csharp/plugin.html

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 20 '05 #3
Nak
> Sorry about the stupid mistake and thank you for your help!

No probs, glad I could help, BTW it's not my site :-)

Nick.

--
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
"No matter. Whatever the outcome, you are changed."

Fergus - September 5th 2003
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
Nov 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Simon X-Session | last post by:
Hi ! I hope somebody can help me, i'm completly out of mind. My problem: I have a class inherited from Windows.Forms.Control named BasicModule, which implements an interface named IModule...
3
by: Jeff Molby | last post by:
Ok, I've googled long and hard, but I can't find anything relevant on this one. I am now at the mercy of your good nature. <g> I have 3 projects in my solution: One app and 2 libraries. I am...
4
by: Chris | last post by:
I created a control that derives from the System.Windows.Forms.Control namespace. I then created an interface to which this new control must adhere, so that I could require future controls to...
7
by: yufufi | last post by:
lets say we have a 'shape' class which doesn't implement IComparable interface.. compiler doesn't give you error for the lines below.. shape b= new shape(); IComparable h; h=(IComparable)b;...
2
by: cv | last post by:
Hi, I want to create an object from a class I load from an assembly. Then I want to cast that object to a class (an interface) 'known' to the program in order to pass it to other methods that work...
0
by: Greg Conely via .NET 247 | last post by:
I am creating a application that will be using plugins. I am doing this so that when I want to let this application work with another type of dbase system, I only have to write\install one plugin,...
2
by: Enrique Bustamante | last post by:
Casting arrays that works on watch and command window but not in code. My application is casting arrays in a way it should work. To test if I was doing something invalid, I wrote a test code that...
6
by: GarrettD78 | last post by:
Accidently posted this to the wrong group so I am reposting. This is probably a newbie question but I am a little confused about how to go next with my code. I think I want to use a factory pattern...
1
by: pitdog | last post by:
This may not be the group to post this in. However I will try... I am interested in how the .NET runtime actually handles casting. For instance... This will work if you assume ChildProduct is...
7
by: S. Lorétan | last post by:
Hi guys, Sorry for this stupid question, but I don't know why it isn't working. Here is my (example) code: namespace Test { class A { public string Label1; }
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
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...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.