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, not the entire app.
I have created a base interface, it looks like:
public interface myModel
sub Initialize(byref DisplayPanel)
sub Connect()
sub Disconnect()
sub Query()
...
end interface
I have created 3 projects. The container, the plugin and the model. I have put this interface file in all three projects. The third project does not do anything, but stores the model for me.
I have created a plugin, it lookes like:
public class myPlugin1
implements myModel
private dbConn as ???? - this is an object of perticular type.
private ResultCount as long
private isConnected as boolean
public sub Initialize(byref DisplayPanel as panel) implements myModel.Initialize
....
end sub
(and so forth)
end class
I build this as a DLL.
I then make my "container" application. I use the following code to load\test for my plugins:
Public Class PluginServices
Public Structure AvailablePlugin
Public AssemblyPath As String
Public ClassName As String
End Structure
Public Shared Function FindPlugins(ByVal strPath As String, ByVal strInterface As String) As AvailablePlugin()
Dim Plugins As ArrayList = New ArrayList
Dim strDLLs() As String, intIndex As Integer
Dim objDLL As [Assembly]
'Go through all DLLs in the directory, attempting to load them
strDLLs = Directory.GetFileSystemEntries(strPath, "*.dll")
For intIndex = 0 To strDLLs.Length - 1
Try
objDLL = [Assembly].LoadFrom(strDLLs(intIndex))
ExamineAssembly(objDLL, strInterface, Plugins)
Catch e As Exception
'Error loading DLL, we don't need to do anything special
End Try
Next
'Return all plugins found
Dim Results(Plugins.Count - 1) As AvailablePlugin
If Plugins.Count <> 0 Then
Plugins.CopyTo(Results)
Return Results
Else
Return Nothing
End If
End Function
Private Shared Sub ExamineAssembly(ByVal objDLL As [Assembly], ByVal strInterface As String, ByVal Plugins As ArrayList)
Dim objType As Type
Dim objInterface As Type
Dim Plugin As AvailablePlugin
'Loop through each type in the DLL
For Each objType In objDLL.GetTypes
'Only look at public types
If objType.IsPublic = True Then
'Ignore abstract classes
If Not ((objType.Attributes And TypeAttributes.Abstract) = TypeAttributes.Abstract) Then
'See if this type implements our interface
objInterface = objType.GetInterface(strInterface, True)
If Not (objInterface Is Nothing) Then
'It does
Plugin = New AvailablePlugin
Plugin.AssemblyPath = objDLL.Location
Plugin.ClassName = objType.FullName
Plugins.Add(Plugin)
End If
End If
End If
Next
End Sub
Public Shared Function CreateInstance(ByVal Plugin As AvailablePlugin) As Object
Dim objDLL As [Assembly]
Dim objPlugin As Object
Try
'Load dll
objDLL = [Assembly].LoadFrom(Plugin.AssemblyPath)
'Create and return class instance
objPlugin = objDLL.CreateInstance(Plugin.ClassName)
Catch e As Exception
Return Nothing
End Try
Return objPlugin
End Function
End Class
I then try to connect to my plugin later in my "container" application. I do this with:
Private Sub lstSourceSystem_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstSourceSystem.SelectedIndexChanged
' Here we connect to the ImportController and show its connection properties
Try
Dim tmpControl As SourceModel
Dim dblResult As Double
**>> tmpControl = DirectCast(PluginServices.CreateInstance(Plugins(l stSourceSystem.SelectedIndex)), myModel)
tmpControl.Initialize(objHost)
frmSystemConnection.Show()
tmpControl.ShowInterface(Me.frmSystemConnection)
Catch ex As Exception
MsgBox(ex.Message)
frmSystemConnection.Hide()
ImportControl = Nothing
End Try
End Sub
I get a cast error on the line starting with **>>
I have tried just using CAST, and tried not using any type of casting. same results.
What am I missing. I have been searching the internet, and I have found examples of how I should do this, but I keep getting the same results.
--------------------------------
From: Greg Conely
-----------------------
Posted by a user from .NET 247 (http://www.dotnet247.com/)
<Id>OUzq1+c3Z0SigQG9wJ15AQ==</Id>