Reflection with plugin...

lidds

Well-known member
Joined
Oct 19, 2004
Messages
122
Programming Experience
Beginner
I have written a plugin application into a third party 3D design software. The 3D design software is owned by another company.

The problem that I have is that with each version of the 3D design software that is released, they add new code and change the version of the dll's that I reference within my plugin app. Therefore I have to completely copy my plugin Visual Studio project, and reference the new version of their dll's and recompile. The problem with this approach is that I then have multiple Visual Studio projects to manage. Because I am only using one function in the API dll, I am 99% sure that they will not change this (an assumption I know).

I want my plugin application to load what ever version of the dll is on the users machine, not the one that I have compiled my application with. Now I have tried setting the "Specific Version" and "Copy Local" property on the dll's to false and this does not work, as it returns the "unable to load dll version ...." error messgae.

One of my friends mention using reflection to load the dll's at runtime, which seems perfect and have tested this using a dll I created to test the approach, and all seemed to work fine. However I have hit a problem with my plugin as this uses an "Implement" from the thrid party dll, and therefore because this is done I am unsure where I can put my reflection code so the dll is reflected prior to the "Implement" code is run.

I hope this makes sense? Below is my code from 2 clases that have to exist within my plugin so as to create a docking form within the thrid party application:

NETAddin.vb
Creates and registers my plugin with the 3D design software and then calls my NetCmd.vb class to create the docking form
VB.NET:
Imports ThirdParty.ApplicationFramework
Imports ThirdParty.ApplicationFramework.Presentation

Public Class NETAddin
    Implements IAddin ' This is from the third party dll

    ' Set the DESCRIPTION string for the CLASS
    ReadOnly Property Description() As String Implements IAddin.Description
        Get
            Return "Myplugin Description"
        End Get
    End Property

    ' Set the NAME string for the CLASS
    ReadOnly Property Name() As String Implements IAddin.Name
        Get
            Return "Myplugin Name"
        End Get
    End Property

    ' When the ADDIN is started, register the command with the command manager

    Public Sub Start(ByVal serviceManager As ServiceManager) Implements IAddin.Start
        ' Get instances of the COMMANDMANAGER and WINDOWMANAGER        
        RISData.Instance.CommandManager = serviceManager.GetService(GetType(CommandManager))
        RISData.Instance.WindowManager = serviceManager.GetService(GetType(WindowManager))

        ' Declare the NETCMD and register it with the COMMANDMANAGER
        Dim CreatePanel As NETCmd = New CreatePanelNETCmd(RISData.Instance.WindowManager)
        RISData.Instance.CommandManager.Commands.Add(CreatePanel)
    End Sub

    ' For completeness, implement the STOP Sub, but it is left empty
    Public Sub [Stop]() Implements IAddin.Stop
    End Sub
End Class


NETCmd.vb
This create a docking form with a panel in it
VB.NET:
Imports ThirdParty.ApplicationFramework.Presentation
Imports System.Windows.Forms

Public Class NETCmd
    Inherits Command

    ' Store the private members for the class
    Private mForm As DockedWindow
    Private bSkip As Boolean = False

    ' Constructor method, supplied with an instance of the WINDOWMANAGER

    Public Sub New(ByRef wndManager As WindowManager)
        ' Update the KEY of the COMMAND (must be unqiue for all the loaded commands)
        MyBase.Key = "MyCompany.MyPlugin.Main"

        ' Add an EVENT to look out for when the window is loaded
        AddHandler wndManager.WindowLayoutLoaded, AddressOf OnWindowLayoutLoaded

        ' Create the CONTROL and set the docking position
        Dim control As UserControl = New frmCreateComment
        Dim docking As DockedPosition = DockedPosition.Right

        ' Create a new DOCKED window in the WINDOWMANAGER
        mForm = wndManager.CreateDockedWindow("MyCompany.MyPlugin.Main", "Plugin Title", control, docking)

        ' Allow the form to be floated and set the initial width
        mForm.Floatable = True
        mForm.Width = 435
        mForm.SaveLayout = False

        ' Add an EVENT to look out for when the window is closed
        AddHandler mForm.Closed, AddressOf OnFormClosed

        ' Sets value so that  knows if RIS has been loaded and ignores error on close
        RISData.Instance.RISLoaded = False
    End Sub

    ' When the control is loaded, ensure the CHECKED property is TRUE
    Sub OnWindowLayoutLoaded(ByVal sender As Object, ByVal e As EventArgs)
        Me.Checked = mForm.Visible
    End Sub

    ' When the CONTROL is closed, ensure the CHECKED property is FALSE
    Sub OnFormClosed(ByVal sender As Object, ByVal e As EventArgs)
        ' to override problem in 12.0.SP5
        Me.bSkip = True
        Me.Checked = False
        Me.bSkip = False
        RISData.Instance.WindowManager.Windows("MyCompany.MyPlugin.ViewComments").Visible = False
    End Sub

    ' The CHECKED property is used by toggle gadgets on toolbars
    Public Overrides Property Checked() As Boolean
        Get
            Return mForm.Visible
        End Get

        Set(ByVal value As Boolean)
            MyBase.Checked = value
        End Set
    End Property

    ' When the COMMAND is executed, either hide or show the FORM
    Public Overrides Sub Execute()
        ' If BSKIP is TRUE, stop
        If bSkip Then
            Return
        End If

        ' If the FORM is visible, hide it and vice versa
        If mForm.Visible Then
            mForm.Hide()
        Else
            mForm.Show()
        End If

        ' Execute standard method on the base class
        MyBase.Execute()
    End Sub
End Class
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,400
Location
Sydney, Australia
Programming Experience
10+
You can't use Reflection if you are implementing an interface from the library. Reflection will be used when assemblies are loaded dynamically, i.e. at run time. Implementing an interface requires binding the assembly at compile time.
 
Top Bottom