Casting an object as an Interface that it implements

J

Jeff Molby

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\BookWorksInventory.dll")
CommonAssembly =
System.Reflection.Assembly.LoadFrom("C:\Documents and Settings\mjozwik_2\My
Documents\Visual Studio
Projects\BookWorks\BookWorksCommon\bin\BookWorksCommon.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
 
N

Nak

Hi there Jeff,

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

http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=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
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
 
J

Jeff Molby

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!
 
N

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
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top