• Mono addin 学习笔记 1


    Mono Addin是一个开源的插件框架,其主要支持的特性如下:

    The main features of Mono.Addins are:

    • Supports descriptions of add-ins using custom attributes (for simple and common extensions) or using an xml manifest (for more complex extensibility needs).
    • Support for add-in hierarchies, where add-ins may depend on other add-ins.
    • Lazy loading of add-ins.
    • Provides an API for accessing to add-in descriptions, which will allow building development and documentation tools for handling add-ins.
    • Dynamic activation / deactivation of add-ins at run time.
    • Allows sharing add-in registries between applications, and defining arbitrary add-in locations.
    • Allows implementing extensible libraries.
    • Supports add-in localization.
    • In addition to the basic add-in engine, it provides a Setup library to be used by applications which want to offer basic add-in management features to users, such as enabling/disabling add-ins, or installing add-ins from on-line repositories.

    (来自mono官方网站的解释,http://www.mono-project.com/Mono.Addins

    大概意思是:

    1. 支持用自定义属性(Attribute,简单项目)和xml定义(大型项目);

    2. 支持插件层次结构,允许插件之间相互依赖(插件本身可以定义扩展点供其他插件进行扩展)

    3. 支持插件懒加载;

    4. 提供了访问插件描述的API接口;

    5. 支持运行期激活/关闭插件;

    6. 主持在应用程序之间共享插件,也允许插件私有;

    7. 支持可扩展的类库;

    8. 本地化支持;

    9. 除了提供插件引擎外,还提供了安装工具,用于提供安装/卸载插件、启用/禁用插件

     下面分析用Attribute声明的方式来进行插件定义

    定义扩展点如下:

     [TypeExtensionPoint] //定义扩展点的属性
     public interface ISnippetProvider
     {
      string GetText (string shortcut);
     }

    下面定义基于该扩展点的扩展如下:

      [Extension] // 扩展点属性
     class StockSnippetProvider: ISnippetProvider
     {
         public string GetText (string shortcut)
        {
                foreach (ExtensionNode<SnippetAttribute> node in AddinManager.GetExtensionNodes("/SnippetsAddinNode/StockSnippets"))
                {
                      if (node.Data.Shortcut == shortcut)
                          return node.Data.Text;
                 }
               return null;
         }
     }

    //定义扩展属性如下:

     [AttributeUsage (AttributeTargets.Assembly, AllowMultiple=true)]  

    public class SnippetAttribute : CustomExtensionAttribute  

    {   

           public SnippetAttribute ()   

           {   }

           public SnippetAttribute ([NodeAttribute ("Shortcut")] string shortcut, [NodeAttribute ("Text")] string text)  

           {   

                Shortcut = shortcut;            

                Text = text;   

           }

          [NodeAttribute]  

          public string Shortcut { get; set; }

          [NodeAttribute]  

          public string Text { get; set; }  

    }

    到目前位置,扩展点和扩展都已经定义好了,现在使用属性来生命扩展点,并定义几个扩展:

    声明扩展点如下:

    [assembly: ExtensionPoint("/SnippetsAddinNode/StockSnippets", ExtensionAttributeType = typeof(SnippetAttribute))]

    声明扩展如下:

    [assembly: Snippet("foreach", "foreach (var item in col) { <|> }")]

    [assembly: Snippet("for", "for (int n=0; n<len; n++) { <|> }")]

    [assembly: Snippet("c", "hello,world!")]

    使用扩展点的代码如下:

    foreach (ISnippetProvider provider in AddinManager.GetExtensionObjects <ISnippetProvider>())

    {     

           string fullText = provider.GetText (word);    

    }

    下一篇将分析用xml描述文件的方式来定义插件

  • 相关阅读:
    React学习 之 阶段性小作品(待办事项_已完成事项 CRUD)
    React 学习之 键盘事件 表单事件 事件对象以及React中的ref获取dom节点 、React实现类似Vue的双向数据绑定
    mysql 时间戳 今天 昨天 7天 30天 及未来时间
    mysql历史数据自动归档
    配置nginx支持跨域访问
    AWK 简明教程
    shell 生成指定范围随机数与随机字符串
    Linux下Shell的for循环语句
    系统部署安装工具 WinToHDD Enterprise v4.2
    格式工厂 FormatFactory v5.1.0.0
  • 原文地址:https://www.cnblogs.com/lenmom/p/3594035.html
Copyright © 2020-2023  润新知