【问题标题】:Dynamically loading .NET assemblies动态加载 .NET 程序集
【发布时间】:2009-05-25 07:49:00
【问题描述】:

我正在编写一个程序,我希望该程序支持插件。我创建了一个程序必须实现的接口。我正在使用我的主程序中的以下代码来调用插件:

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)

一切正常,我可以访问我公开的属性,除了一个我无法弄清楚的问题。在我的插件中,我使用以下代码来公开一个属性:

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

好的,所以我从我的主程序中设置了这个属性,一切正常。除了,过了一会儿,m_PanelObject 出于某种奇怪的原因被设置为 Nothing。我没有在程序中的任何地方将其设置为 Nothing,插件代码中也没有将其设置为 Nothing 的位置。那么我在这里错过了什么?我相信这将是非常明显的。提前致谢

【问题讨论】:

  • 您能否提供一个来自您的主程序的示例,特别是抛出异常的行的位置?
  • 从主程序调用以下行: Plugin.Plugin.DataWriteHeader(FileWriter, "TestTable") 这是插件中的一个子程序。插件本身就是引发异常的地方。

标签: c# .net vb.net assemblies


【解决方案1】:

首先,看看MEF,因为你正在无缘无故地重新发明一个轮子。

至于您最初的问题,请确保您的插件实例没有被丢弃或 PanelObject 被分配 Nothing 或类似的东西:.NET 垃圾收集器与此问题无关。

【讨论】:

  • 我不使用 MEF 的原因是,据我了解,它是针对 .NET 3.5 或 .NET 4.0 的。我的应用程序面向 .NET 2.0
  • 不,MEF 也可以与 .NET 2.0 一起使用。
  • 那我去看看。感谢您的意见。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-15
  • 1970-01-01
  • 2016-10-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多