mooman_fl
Centurion
This is related to the plugin project I mentioned on the other thread.
The app is set up as a User Control that find and loads plugins. The main interface it checks for to determine a valid plugin is within the control assembly. The plugin references this interface (IPlug.IPluginInformation) as does the class that I am trying to DirectCast to. I do one thing a little out of the ordinary however: I load a new AppDomain and this part of the code is within the second AppDoman. It iterates through the Types in the plugin assembly just fine, and correctly determins that it implements the desired interface. I can use Assembly.CreateInstance to get a reference to the Class Object, however when I try to DirectCast it to the PluginInformation class that also Implements the same interface, I get:
Unable to cast object of type 'Plugin1.PluginInfo' to type 'IPlug.IPlugInfo'.
Plugin1.PluginInfo is the name of the class in the plugin that implements the IPlug.IPlugInfo interface. I am a little stumped as to what is the problem. Is it because I am doing this in second AppDomain? If so, why is it even recognizing the interface to begin with? Here is the code for the method that does this:
Any help would be appreciated.
The app is set up as a User Control that find and loads plugins. The main interface it checks for to determine a valid plugin is within the control assembly. The plugin references this interface (IPlug.IPluginInformation) as does the class that I am trying to DirectCast to. I do one thing a little out of the ordinary however: I load a new AppDomain and this part of the code is within the second AppDoman. It iterates through the Types in the plugin assembly just fine, and correctly determins that it implements the desired interface. I can use Assembly.CreateInstance to get a reference to the Class Object, however when I try to DirectCast it to the PluginInformation class that also Implements the same interface, I get:
Unable to cast object of type 'Plugin1.PluginInfo' to type 'IPlug.IPlugInfo'.
Plugin1.PluginInfo is the name of the class in the plugin that implements the IPlug.IPlugInfo interface. I am a little stumped as to what is the problem. Is it because I am doing this in second AppDomain? If so, why is it even recognizing the interface to begin with? Here is the code for the method that does this:
Visual Basic:
Friend Function GetPluginInfo(ByVal FullPath As String) As PluginInformation
Dim tempAssembly As [Assembly]
Dim IPlugName As String = GetType(IPlug.IPlugInfo).ToString
Dim temp As Type
Dim tempRef As Object
Dim tempPlugInfo As PluginInformation
tempAssembly = [Assembly].LoadFile(FullPath)
For Each Exported As Type In tempAssembly.GetTypes 'cycle through the types that are in the assembly
If Exported.IsPublic Then
If Not ((Exported.Attributes And TypeAttributes.Abstract) = TypeAttributes.Abstract) Then
temp = Exported.GetInterface(IPlugName, True) 'checks for a match against default interface
If Not (temp Is Nothing) Then 'if it is there, set the flag
tempRef = tempAssembly.CreateInstance(Exported.FullName)
'when it hits the following line it gives the error
tempPlugInfo = DirectCast(tempRef, IPlug.IPlugInfo)
End If
End If
End If
Next
Return tempPlugInfo
End Function
Any help would be appreciated.