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