Jump to content

Recommended Posts

Posted

im running a simple windows app with plugins, but in order for the plugins to work i first need this piece of code to work. In theory it should work fine but in practice... well not quite. I have a similar app with the exact same piece of code and all is working fine so im really confused.... the error is generated by the Bolded code, i think the error message is something like "not all types can be loaded". if anyone can solve this issue id really appreciate it. shot

Checks the specified directory for plugins of the correct type and then adds them to an array of availablePlugins

Public Shared Function findPlugins(ByVal pluginPath As String, ByVal pluginInterfaceName As String) As availablePlugin()

Dim plugins As New ArrayList

Dim pluginDLL() As String

Dim index As Integer

Dim objDLL As [Assembly]

'Go through all DLLs in the directory, attempting to load them

pluginDLL = Directory.GetFileSystemEntries(pluginPath, "*.dll")

For index = 0 To pluginDLL.Length - 1

MessageBox.Show(pluginDLL(index))

Try

objDLL = [Assembly].LoadFrom(pluginDLL(index))

examineAssembly(objDLL, pluginInterfaceName, plugins)

Catch e As Exception

MessageBox.Show("Debug")

'There has been an error in loading the DLL. No need to add any code here

End Try

Next

'Return all working plugins found

Dim pluginsFound(plugins.Count - 1) As availablePlugin

If plugins.Count <> 0 Then

plugins.CopyTo(pluginsFound)

Return pluginsFound

Else

Return Nothing

End If

End Function

'Check to see if the plugin implements the correct interface and if it does then add it to the list of availablePlugins

Private Shared Sub examineAssembly(ByVal objDLL As [Assembly], ByVal pluginInterfaceName As String, ByVal plugins As ArrayList)

Dim objType As Type

MessageBox.Show("1")

Dim objTypeA() As Type = objDLL.GetTypes

MessageBox.Show("2")

Dim objInterface As Type

Dim plugin As availablePlugin

'Go through all types in the DLL to see if they are the required type

Try

For Each objType In objTypeA

'We only want Public types since that is how our types are defined

If objType.IsPublic = True Then

'We dont want abstract classes so...

If Not ((objType.Attributes And TypeAttributes.Abstract) = TypeAttributes.Abstract) Then

'We want to see if it implements pluginInterface

objInterface = objType.GetInterface(pluginInterfaceName, True)

If Not (objInterface Is Nothing) Then

'Since it does implement pluginInterface we add it to our array of availablePlugins

plugin = New availablePlugin

plugin.pluginPath = objDLL.Location

plugin.pluginName = objType.FullName

plugins.Add(plugin)

End If

End If

End If

Next

Catch e As Exception

MessageBox.Show(e.Message)

End Try

End Sub


Posted

It would be more helpful if you attached your project to post so others can open it and look. If quoting error messages, you need to be exact, not "is something like".

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...