简体   繁体   English

动态加载.NET程序集

[英]Dynamically loading .NET assemblies

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. 除了一段时间之后,出于某种奇怪的原因,m_PanelObject被设置为Nothing。 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. 我没有在程序中的任何地方将其设置为Nothing,并且在插件代码中也没有位置将其设置为Nothing。 So what am I missing here? 那我在这里想念什么? I am sure this will be pretty obvious. 我相信这将是显而易见的。 Thanks in advance 提前致谢

First, take a look at MEF because you're reinventing a wheel for no apparent reason. 首先,请看一下MEF,因为您没有明显的理由在重新发明轮子。

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. 至于您的原始问题,请确保不会丢弃您的插件实例或为PanelObject分配“ Nothing或诸如此类的东西:.NET Garbage Collector与该问题无关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM