Reflection and Plugins
Hi,
I am starting out creating an app that I would like to dynamically load dlls that would be developed, plugins. I am using the C++.NET framework.
My question is, Is Relection the way to do this?
I have been searching for examples in C++.NET but most seem to be in C# or VB.NET.
Can any point me in the best direction? Examples? Relection?
Thanks
Jeff
[375 byte] By [
Hooper] at [2007-12-17]
you got it.. here is the code I use, it's VB and can be easely converted to C++
-
Imports System.IO
Imports System.Reflection
Public Class PluginEngine
Public Shared Function FindPlugins(ByVal path As String, ByVal interface_name As String) As ArrayList
Dim plugins As ArrayList
Dim dll_list() As String
Dim index As Integer
Dim cur_dll As [Assembly]
plugins = New ArrayList
dll_list = Directory.GetFileSystemEntries(path, "*.dll")
For index = 0 To dll_list.Length - 1
Try
cur_dll = [Assembly].LoadFrom(dll_list(index))
ExamineAssembly(cur_dll, interface_name, plugins)
Catch e As Exception
End Try
Next
Return plugins
End Function
Private Shared Sub ExamineAssembly(ByVal cur_dll As [Assembly], ByVal interface_name As String, ByVal plugins As ArrayList)
Dim cur_type As Type
Dim cur_interface As Type
Dim plugin As AvailablePlugin
For Each cur_type In cur_dll.GetTypes
If cur_type.IsPublic = True Then
If Not ((cur_type.Attributes And TypeAttributes.Abstract) = TypeAttributes.Abstract) Then
cur_interface = cur_type.GetInterface(interface_name, True)
If Not (cur_interface Is Nothing) Then
plugin = New AvailablePlugin
plugin.AssemblyPath = cur_dll.Location
plugin.FullClassName = cur_type.FullName
plugins.Add(plugin)
End If
End If
End If
Next
End Sub
Public Shared Function CreateInstance(ByVal plugin As AvailablePlugin) As Object
Dim cur_dll As [Assembly]
Dim cur_plugin As Object
Try
cur_dll = [Assembly].LoadFrom(plugin.AssemblyPath)
cur_plugin = cur_dll.CreateInstance(plugin.FullClassName)
Catch e As Exception
Throw e ''Return Nothing
End Try
Return cur_plugin
End Function
End Class
--
Public Class AvailablePlugin
Private _assembly_path As String
Private _class_name As String
Public Property AssemblyPath() As String
Get
Return _assembly_path
End Get
Set(ByVal Value As String)
_assembly_path = Value
End Set
End Property
Public Property FullClassName() As String
Get
Return _class_name
End Get
Set(ByVal Value As String)
_class_name = Value
End Set
End Property
Public ReadOnly Property ClassName() As String
Get
Dim index As Integer
index = _class_name.LastIndexOf(".")
If index > 0 Then
Return _class_name.Substring(index + 1)
End If
Return _class_name
End Get
End Property
End Class
To add to the information
interface_name is the actual class name of an interface
Public Interface ICell
Property Data() As Object
Property X() As Integer
Property Y() As Integer
Property Style() As String
Sub ProcessCell()
End Interface
interface_name = "YourNamespace.ICell"
Because each plugin must inherit from an interface to have the same basic signature.
This also help the developper of the plugin, cause he will know what function you will call.