【问题标题】:Importing into a Exported object with MEF使用 MEF 导入导出的对象
【发布时间】:2011-03-02 02:17:13
【问题描述】:

如果这个问题已经被问了 100 次,我很抱歉,但我真的很难让它发挥作用。

假设我有三个项目。

  • Core.dll
    • 具有通用接口
  • Shell.exe
    • 加载程序集文件夹中的所有模块。
    • 参考 Core.dll
  • ModuleA.dll
    • 导出名称、模块版本。
    • 参考 Core.dll

Shell.exe 有一个 [Export],其中包含我需要注入到所有加载的模块中的第三方应用程序的单个实例。

到目前为止,我在 Shell.exe 中的代码是:

static void Main(string[] args)
{
        ThirdPartyApp map = new ThirdPartyApp();

        var ad = new AssemblyCatalog(Assembly.GetExecutingAssembly());
        var dircatalog = new DirectoryCatalog(".");
        var a = new AggregateCatalog(dircatalog, ad);

        // Not to sure what to do here.
}

class Test
{
    [Export(typeof(ThirdPartyApp))]
    public ThirdPartyApp Instance { get; set; }

    [Import(typeof(IModule))]
    public IModule Module { get; set; }
}

我需要创建一个 Test 实例,并从 Main 方法加载 Instancemap,然后从执行目录中的 ModuleA.dll 加载模块,然后 [Import] Instance 到加载模块。

ModuleA我有一个这样的课程:

[Export(IModule)]
class Module : IModule
{
    [Import(ThirdPartyApp)]
    public ThirdPartyApp Instance {get;set;}
}

我知道我已经完成了一半我只是不知道如何将它们放在一起,主要是使用来自Mainmap 实例加载测试。

谁能帮帮我。

【问题讨论】:

    标签: c# ioc-container mef


    【解决方案1】:

    我似乎可以让它在Test类的构造函数中工作(是的,我知道不能在构造函数中工作,我会把它移出):

    public Test()
        {
            ThirdPartyApp map = new ThirdPartyApp();
            this.MapInfoInstance = map;
    
            //What directory to look for!
            String strPath = AssemblyDirectory;
            using (var Catalog = new AggregateCatalog())
            {
                DirectoryCatalog directorywatcher = new DirectoryCatalog(strPath, "*.dll");
                Catalog.Catalogs.Add(directorywatcher);
                CompositionBatch batch = new CompositionBatch();
                batch.AddPart(this);
                CompositionContainer container = new CompositionContainer(Catalog);
                //get all the exports and load them into the appropriate list tagged with the importmany
                container.Compose(batch);
    
                foreach (var part in Catalog.Parts)
                {
                    container.SatisfyImportsOnce(part);
                }
            }
    
            Module.Run();
        }
    

    【讨论】:

      【解决方案2】:

      你的主要方法应该是这样的:

      static void Main(string[] args)
      {   
          var exeCatalog = new AssemblyCatalog(Assembly.GetExecutingAssembly());
          var dircatalog = new DirectoryCatalog(".");
          var aggregateCatalog = new AggregateCatalog(exeCatalog, dirCatalog);
          var container = new CompositionContainer(aggregateCatalog);
          var program = container.GetExportedValue<Program>();
      
          program.Run();
      }
      

      要使ThirdPartyApp 类的实例可用作可由模块导入的部件,您有两种选择。第一种是使用ComposeExportedValue 扩展方法将这样的实例显式添加到容器中,如下所示:

      container.ComposeExportedValue<ThirdPartyApp>(new ThirdPartyApp());
      

      或者,您可以通过属性导出它,方法是使用类似这样的类:

      public class ThirdPartyAppExporter
      {
          private readonly ThirdPartyApp thirdPartyApp = new ThirdPartyApp();
      
          [Export]
          public ThirdPartyApp ThirdPartyApp { get { return thirdPartyApp; } }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-01-07
        • 1970-01-01
        • 1970-01-01
        • 2011-12-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多