zoukankan      html  css  js  c++  java
  • MEF在运行时替换插件

    利用AppDomain的ShadowCopy特性。

     var setup = new AppDomainSetup
            {
                CachePath = cachePath,
                ShadowCopyFiles = "true",
                ShadowCopyDirectories = pluginPath
            };
    

     创建程序集

    domain = AppDomain.CreateDomain("Host_AppDomain", AppDomain.CurrentDomain.Evidence, setup); 
    var asmfullname = typeof(UseConfig).Assembly.FullName;
    var typefullname = typeof(UseConfig).FullName;
    useConfig = (UseConfig)domain.CreateInstanceAndUnwrap(asmfullname, typefullname);
    useConfig.Build();

     在创建好的程序集里通过MEF加载插件

    useConfig.cs: public void Build() 
    { 
    var regBuilder = new RegistrationBuilder(); 
    regBuilder.ForTypesDerivedFrom<IConfig>().Export<IConfig>(); 
    var catalog = new AggregateCatalog(); 
    catalog.Catalogs.Add(new AssemblyCatalog(typeof(UseConfig).Assembly, regBuilder)); 
    directoryCatalog = new DirectoryCatalog(pluginPath, regBuilder);
    catalog.Catalogs.Add(directoryCatalog); container = new CompositionContainer(catalog);
    container.ComposeExportedValue(container); configs = container.GetExportedValues<IConfig>(); }

    替换插件

    1. 先删除后添加
      刷新一下,重新输出。
      public void Recompose()
      {
      directoryCatalog.Refresh();
      container.ComposeParts(directoryCatalog.Parts);
      configs = container.GetExportedValues<IConfig>();
      }

    2. 直接替换
      通过FileWatch监视,一旦有文件变化Unload AppDomain,然后重新加载。

  • 相关阅读:
    Find and Grep
    Device trees, Overlays and Parameters of Raspberry Pi
    Ubuntu终端常用的快捷键
    ARM Linux 3.x的设备树(Device Tree)
    Linux GPIO子系统
    Linux Pin Control 子系统
    JavaScript运算符优先级
    JavaScript中的严格模式
    小程序云开发调用HTTP请求中got第三方库使用失败解决方法
    小程序生命周期函数
  • 原文地址:https://www.cnblogs.com/zeroone/p/9043500.html
Copyright © 2011-2022 走看看