views:

451

answers:

1

Guys,

I am writing a program and I want the program to support plugins. I created an interface that the program must implement. I am using the following code from my main program to call the plugin:

Dim asm As Assembly = Assembly.LoadFrom(ff.FullName)
' Get the type
Dim myType As System.Type = asm.GetType(asm.GetName.Name + "." + asm.GetName.Name)
' If the type is null then we try again without the root namespace name
If myType Is Nothing Then myType = asm.GetType(asm.GetName.Name)
' Check to see if the plugin implements the required Interface
Dim IsMyPlugin As Boolean = GetType(LGInterfaces.ILGSQLPlugin).IsAssignableFrom(myType)
Dim ActivePlugin As New PluginObject()
If IsMyPlugin Then
    ActivePlugin.Plugin = CType(Activator.CreateInstance(myType), LGInterfaces.ILGPlugin)

Every thing works and I am able to access my exposed properties, except for one problem I can not figure out. In my plugin, I use the following code to expose a property:

Private m_PanelObject As Windows.Forms.Control

Public Property PanelObject() As Windows.Forms.Control
Get
     Return m_PanelObject
End Get
Set(ByVal value As Windows.Forms.Control)
    m_PanelObject = value
End Set

Ok, so I set this property from my main program and everything works. Except, after a while, m_PanelObject gets set to Nothing for some odd reason. I'm not setting it to Nothing anywhere in my program and there is no place in the plugin code that sets it to Nothing. So what am I missing here? I am sure this will be pretty obvious. Thanks in advance

+3  A: 

First, take a look at MEF because you're reinventing a wheel for no apparent reason.

As for your original question, make sure your plugin instance does not get disposed of or PanelObject is assigned Nothing or something of the kind: .NET Garbage Collector has nothing to do with this problem.

Anton Gogolev
The reason I'm not using MEF is because, as I understand it, it is targeted for .NET 3.5 or .NET 4.0. My application targets .NET 2.0
No, MEF can be used with .NET 2.0 as well.
Anton Gogolev
I will check it out then. Thanks for the input.