RandyB30
10-25-2008, 11:34 PM
I have an interface "IPlugin" located in project PluginInterface. "PartApprovalPlugin" implements this interface in the project PartApprovalPlugin. I am trying to make a generic function for adding plugins to a collection that is passed in. It should work for any type of plugin that is passed to it. I am working with a code example I found on the internet and have modified it to work for me. If I declare a variable like this:
Dim clsPlugin As PluginInterface.IPlugin
everything works fine but of course it wont work for other types of plugins that are passed in. So what I need is to be able to return the type of the interface so i can create a variable and an instance of that interface. The code I have so far is below.
Public Function LoadPlugin(ByVal FilePath As String, ByVal InterfaceType As String, ByVal PluginCollection As Collection) As Boolean
Dim clsAssembly As System.Reflection.Assembly
Dim clsInterface As Type
Dim bAdded As Boolean = False
Try
clsAssembly = System.Reflection.Assembly.LoadFrom(FilePath)
Catch
Throw New System.Exception("An error occured while attempting to access the specified Assembly.")
End Try
Try
'look for appropriate types...
For Each clsType As Type In clsAssembly.GetTypes
Debug.Print(clsType.ToString)
'only look at types we can create...
If clsType.IsPublic = True Then
'ignore abstract classes...
If Not ((clsType.Attributes And System.Reflection.TypeAttributes.Abstract) = System.Reflection.TypeAttributes.Abstract) Then
'check for the implementation of the specified interface...
clsInterface = clsType.GetInterface(InterfaceType, True)
'process if valid...
If Not (clsInterface Is Nothing) Then
'create a unique key to identify this plugin (so we don't add it twice)...
Dim sPluginKey As String = String.Concat(FilePath, "_", clsType.FullName)
' Check to see if we already have this plugin added...
If Not PluginCollection.Contains(sPluginKey) Then
' The next line works fine but I would rather get the type
' PluginInterface.IPlugin instaed of hardcoding the type.
' That way this function can be used for other types of plugins.
Dim clsPlugin As PluginInterface.IPlugin = CType(clsAssembly.CreateInstance(clsType.FullName), PluginInterface.IPlugin)
PluginCollection.Add(clsPlugin, clsPlugin.PluginType, Nothing, Nothing)
bAdded = True
End If
End If
End If
End If
Next
Return bAdded
Catch ex As System.Exception
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Function
Thanks,
Randy
Dim clsPlugin As PluginInterface.IPlugin
everything works fine but of course it wont work for other types of plugins that are passed in. So what I need is to be able to return the type of the interface so i can create a variable and an instance of that interface. The code I have so far is below.
Public Function LoadPlugin(ByVal FilePath As String, ByVal InterfaceType As String, ByVal PluginCollection As Collection) As Boolean
Dim clsAssembly As System.Reflection.Assembly
Dim clsInterface As Type
Dim bAdded As Boolean = False
Try
clsAssembly = System.Reflection.Assembly.LoadFrom(FilePath)
Catch
Throw New System.Exception("An error occured while attempting to access the specified Assembly.")
End Try
Try
'look for appropriate types...
For Each clsType As Type In clsAssembly.GetTypes
Debug.Print(clsType.ToString)
'only look at types we can create...
If clsType.IsPublic = True Then
'ignore abstract classes...
If Not ((clsType.Attributes And System.Reflection.TypeAttributes.Abstract) = System.Reflection.TypeAttributes.Abstract) Then
'check for the implementation of the specified interface...
clsInterface = clsType.GetInterface(InterfaceType, True)
'process if valid...
If Not (clsInterface Is Nothing) Then
'create a unique key to identify this plugin (so we don't add it twice)...
Dim sPluginKey As String = String.Concat(FilePath, "_", clsType.FullName)
' Check to see if we already have this plugin added...
If Not PluginCollection.Contains(sPluginKey) Then
' The next line works fine but I would rather get the type
' PluginInterface.IPlugin instaed of hardcoding the type.
' That way this function can be used for other types of plugins.
Dim clsPlugin As PluginInterface.IPlugin = CType(clsAssembly.CreateInstance(clsType.FullName), PluginInterface.IPlugin)
PluginCollection.Add(clsPlugin, clsPlugin.PluginType, Nothing, Nothing)
bAdded = True
End If
End If
End If
End If
Next
Return bAdded
Catch ex As System.Exception
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Function
Thanks,
Randy