C
Chris Morse
Hi,
I'm trying to figure out how to dynamically load an assembly and get
an IClientPlugin interface to a class. I can get an "Object"
reference to it after doing an "Activator.CreateInstance" call, but is
there any way to get an IClientPlugin reference to it?
Trying to do a "CType(obj, IClientPlugin)" throws an exception.
Here's the code that loads the assembly and calls a method called
"GetPluginName" that takes no parameters and returns a string:
//////////////////// BEGIN CODE /////////////////////////////
Dim szPluginPath As String
Dim objPluginAssembly As System.Reflection.Assembly
Dim objIClientPlugin As IClientPlugin
Dim obj As Object
Dim objResult As Object
Dim szError As String
Dim T As Type
Dim T2 As Type
szPluginPath = "C:\PLUGINS\\ANET_SystemNews.dll"
objPluginAssembly = System.Reflection.Assembly.LoadFile(szPluginPath)
For Each T In objPluginAssembly.GetTypes()
If T.IsClass() = True Then
'Class implements IClientPlugin interface?
If Not IsNothing(T.GetInterface("IClientPlugin")) Then
'YES, so create an instance of this class
obj = Activator.CreateInstance(T)
' **** objPlugin = CType(obj, IClientPlugin)
'Invoke IClientPlugin.GetPluginName()
objResult = T.InvokeMember("GetPluginName", _
Reflection.BindingFlags.Default Or _
Reflection.BindingFlags.InvokeMethod, _
Nothing, _
obj, _
Nothing)
Debug.WriteLine("Result: {0}", objResult)
End If
End If
Next
//////////////////// END CODE /////////////////////////////
I've commented out the line that causes an error, the CType(obj,
IClientPlugin) call. Here is how the client is implemented:
Public Class AppleNET_BBS_PLUGIN
Implements IClientPlugin
... IClientPlugin functions defined ...
End Class
Is there any way I can get an "IClientPlugin" reference to the
instantiated class? It would make it a lot easier to call methods:
Dim objIClientPlugin As IClientPlugin
.. create class through reflection
objIClientPlugin = ????(obj)
Thanks for any help you can offer!
// CHRIS
I'm trying to figure out how to dynamically load an assembly and get
an IClientPlugin interface to a class. I can get an "Object"
reference to it after doing an "Activator.CreateInstance" call, but is
there any way to get an IClientPlugin reference to it?
Trying to do a "CType(obj, IClientPlugin)" throws an exception.
Here's the code that loads the assembly and calls a method called
"GetPluginName" that takes no parameters and returns a string:
//////////////////// BEGIN CODE /////////////////////////////
Dim szPluginPath As String
Dim objPluginAssembly As System.Reflection.Assembly
Dim objIClientPlugin As IClientPlugin
Dim obj As Object
Dim objResult As Object
Dim szError As String
Dim T As Type
Dim T2 As Type
szPluginPath = "C:\PLUGINS\\ANET_SystemNews.dll"
objPluginAssembly = System.Reflection.Assembly.LoadFile(szPluginPath)
For Each T In objPluginAssembly.GetTypes()
If T.IsClass() = True Then
'Class implements IClientPlugin interface?
If Not IsNothing(T.GetInterface("IClientPlugin")) Then
'YES, so create an instance of this class
obj = Activator.CreateInstance(T)
' **** objPlugin = CType(obj, IClientPlugin)
'Invoke IClientPlugin.GetPluginName()
objResult = T.InvokeMember("GetPluginName", _
Reflection.BindingFlags.Default Or _
Reflection.BindingFlags.InvokeMethod, _
Nothing, _
obj, _
Nothing)
Debug.WriteLine("Result: {0}", objResult)
End If
End If
Next
//////////////////// END CODE /////////////////////////////
I've commented out the line that causes an error, the CType(obj,
IClientPlugin) call. Here is how the client is implemented:
Public Class AppleNET_BBS_PLUGIN
Implements IClientPlugin
... IClientPlugin functions defined ...
End Class
Is there any way I can get an "IClientPlugin" reference to the
instantiated class? It would make it a lot easier to call methods:
Dim objIClientPlugin As IClientPlugin
.. create class through reflection
objIClientPlugin = ????(obj)
Thanks for any help you can offer!
// CHRIS