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


Answer this question

Reflection and Plugins

  • JCesar

    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.



  • Binayak67839

    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



  • Ice Romeo

  • btrefonas

    I already posted the code for my plugin engine.

  • Martin Maierhofer

    I got that and I understand all of it but the interface part....it makes no sence how I impliment it into the plugin. I have been trying to get a plugin program working for nearly 2 months and I am just.....lost beyond belief. Your code is the first code that is making at least a little since.

    Thanks

    Marc


  • Jan_B-K

    Thanks much. I will study this over.
  • Utkarsh Shigihalli

    That is something that I am concerned about, making function calls. It seems like the code will not be as readable, self-documenting, as when not using plug-ins. More like making calls via an array index.
    Jeff

  • jirikraus

    Hey ThE_lOtUs can you post an example code for the plugin

    Thanks

    Marc


  • Trebormac

    I am going to be loosing internet here in about 10 min and I dont know when Ill be on again, so thanks ahead of time.

    Marc


  • Reflection and Plugins