加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

在C#中编写Addin或插件框架

发布时间:2020-12-15 08:27:27 所属栏目:百科 来源:网络整理
导读:我正在用C#编写一个Addin框架,我想知道如何在不需要重新启动应用程序的情况下使Addin无法加载. 我听说过AppDomains但这些有用吗? Addin可以添加可扩展性类,并且可以通过接口在主AppDomain中调用,并且仍然可以卸载并调用清理代码,导致这些类被删除而该程序集
我正在用C#编写一个Addin框架,我想知道如何在不需要重新启动应用程序的情况下使Addin无法加载.

我听说过AppDomains但这些有用吗? Addin可以添加可扩展性类,并且可以通过接口在主AppDomain中调用,并且仍然可以卸载并调用清理代码,导致这些类被删除而该程序集不在主AppDomain中吗?

或者是否有其他方法可以实现无法加载的插件,但是除了AppDomain之外的IIRC,你无法卸载组件.

如果可能的话,我还希望插件引擎与Mono兼容,所以如果可以的话,任何答案都会尝试与Mono保持兼容.

解决方法

你可能想看看 Mono.Addins.有一些好看的样品 here,这里有一些细节:

The following code is a basic example of an add-in host application.
This application runs a set of commands which can be implemented by
add-ins:

using System;
using Mono.Addins;

[assembly:AddinRoot ("HelloWorld","1.0")]

class MainClass
{
    public static void Main ()
    {
        AddinManager.Initialize ();
        AddinManager.Registry.Update ();

        foreach (ICommand cmd in AddinManager.GetExtensionObjects<ICommand> ())
            cmd.Run ();
    }
}

The extension point for the above example can be declared like this:

using Mono.Addins;

[TypeExtensionPoint]
public interface ICommand
{
    void Run ();
}

An add-in extending the above extension point would look like this:

using System;
using Mono.Addins;

[assembly:Addin]
[assembly:AddinDependency ("HelloWorld","1.0")]

[Extension]
public class HelloCommand: ICommand
{
    public void Run ()
    {
        Console.WriteLine ("Hello World!");
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读