在C#编写一个插件或插件框架 [英] Writing an Addin or Plugin Framework in C#

查看:1310
本文介绍了在C#编写一个插件或插件框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个插件框架在C#中,我不知道我怎么可以让外接程序的卸载的,而不需要申请重新启动。

我听到的AppDomain,但如何做这些工作?可一个插件添加扩展性类和调用在主AppDomain中通过接口,仍然是可卸载的,并呼吁清理code导致这些类中没有了大会的主要应用程序域是被删除?

还是有实现卸载的加载项的其他方法,但IIRC其他然后AppDomain中的你不能卸载的组件。

我也想了插件引擎与单声道如果可能的话兼容,因此任何答案尽量保持与单声道兼容的,如果可以的话。

解决方案

您可能想看看 Mono.Addins 。有一些漂亮的样品<一个href="http://monoaddins.$c$cplex.com/wikipage?title=Extension%20Points%20and%20Extensions&referringTitle=Programming%20Guide">here,下面是一些细节:

  

下面code是一个附加的主机应用程序的一个基本的例子。   这个应用程序运行的一组命令,可通过实施   加载项:

 使用系统;
使用Mono.Addins;

[总成:AddinRoot(HelloWorld的,1.0)

类MainClass
{
    公共静态无效的主要()
    {
        AddinManager.Initialize();
        AddinManager.Registry.Update();

        的foreach(ICommand的cmd,在AddinManager.GetExtensionObjects&LT; ICommand的&GT;())
            cmd.Run();
    }
}
 

  

在上面的例子中的扩展点可以这样声明:

 使用Mono.Addins;

[TypeExtensionPoint]
公共接口的ICommand
{
    无效的run();
}
 

  

这是外接扩展上面的扩展点是这样的:

 使用系统;
使用Mono.Addins;

[总成:加载项]
[总成:AddinDependency(HelloWorld的,1.0)

[扩展]
公共类HelloCommand:ICommand的
{
    公共无效的run()
    {
        Console.WriteLine(你好世界!);
    }
}
 

I am writing an Addin Framework in C# and I'm wondering how I can make Addin's unloadable without requiring a restart of the application.

I heard of AppDomains but how do these work? Can an Addin add extendability classes and be called in the Main AppDomain by Interfaces and still be unloadable and call cleanup code resulting in those classes being removed without that Assembly being in the Main AppDomain?

Or are there other methods of achieving unloadable addins, but IIRC other then AppDomain's you cannot unload an Assembly.

I also want the addin engine to be compatible with Mono if possible, so any answers try to stay compatible with Mono, if you can.

解决方案

You might want to look at Mono.Addins. There are some good looking samples here, Here are some of the details:.

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!");
    }
}

这篇关于在C#编写一个插件或插件框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆