Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# MEF——我好像遗漏了什么_C#_Mef - Fatal编程技术网

C# MEF——我好像遗漏了什么

C# MEF——我好像遗漏了什么,c#,mef,C#,Mef,所以,我在和MEF玩——有点像是在尝试一个经典的IoC的东西——注入一个日志类。 我有它的工作,但它没有工作的方式,我期待它的工作。你能告诉我我偏离了轨道吗 我认为我的问题是我错误地引导MEF。我希望导出一个记录器,并将其导入到任何想要使用它的类中 我发现,对于任何想要使用它的类,我都必须新建一个容器,加载目录,并调用ComposeParts()。(这实际上比仅仅调用MyEventLogFactory.GetEventLog()困难得多)。(我原以为必须在程序init上引导MEF一次,但我似乎必

所以,我在和MEF玩——有点像是在尝试一个经典的IoC的东西——注入一个日志类。 我有它的工作,但它没有工作的方式,我期待它的工作。你能告诉我我偏离了轨道吗

我认为我的问题是我错误地引导MEF。我希望导出一个记录器,并将其导入到任何想要使用它的类中

我发现,对于任何想要使用它的类,我都必须新建一个容器,加载目录,并调用ComposeParts()。(这实际上比仅仅调用MyEventLogFactory.GetEventLog()困难得多)。(我原以为必须在程序init上引导MEF一次,但我似乎必须为导入中的每个类引导MEF)

以下是我的引导代码(在控制台应用程序启动时):

然后,我的TestHarness类如下所示:


public class TestHarness
{
  [Import(typeof(IEventLog))]
  public IEventLog EventLog { get; set; }

  public void RunTest()
  {
     // did not expect to need to do this, but EventLog is null if I don't
     Program._Container.ComposeParts(this);

     this.EventLog.Write(some test event stuff);
  }
}
static void Main(string[] args)
{
    InitMefContainer();

    var test = _Container.GetExportedValue<TestHarness>();
    test.RunTest();

    Console.ReadLine();
}

private static void InitMefContainer()
{
    var catalog = new AggregateCatalog();
    catalog.Catalogs.Add(new AssemblyCatalog(typeof(EventLogFactory).Assembly));
    catalog.Catalogs.Add(new AssemblyCatalog(typeof(Program).Assembly));
    var container = new CompositionContainer(catalog);
    container.ComposeParts();
    _Container = container;
}

[Export]
public class TestHarness
{
  [Import(typeof(IEventLog))]
  public IEventLog EventLog { get; set; }

  public void RunTest()
  {
     // did not expect to need to do this, but EventLog is null if I don't

     this.EventLog.Write(some test event stuff);
  }
}
我的问题是事件日志总是空的。-除非我重复该程序.Main执行的所有mefinification代码(或者,如果我将该程序创建的容器设置为公共静态,我可以调用_container.ComposeParts(this))


那么,我的问题是:引导MEF以满足所有导入的正确方法是什么?使用公共静态容器并调用container.Compose(this)是常见的做法吗?(或者使用AsaticContainer.GetExportedValue,有点像使用ServiceLocator.GetService()?在我所有的类中——我肯定没有在任何示例代码中看到这一点。

我尝试在引导过程中通过容器注入尽可能多的内容

我是这样想的:


public class TestHarness
{
  [Import(typeof(IEventLog))]
  public IEventLog EventLog { get; set; }

  public void RunTest()
  {
     // did not expect to need to do this, but EventLog is null if I don't
     Program._Container.ComposeParts(this);

     this.EventLog.Write(some test event stuff);
  }
}
static void Main(string[] args)
{
    InitMefContainer();

    var test = _Container.GetExportedValue<TestHarness>();
    test.RunTest();

    Console.ReadLine();
}

private static void InitMefContainer()
{
    var catalog = new AggregateCatalog();
    catalog.Catalogs.Add(new AssemblyCatalog(typeof(EventLogFactory).Assembly));
    catalog.Catalogs.Add(new AssemblyCatalog(typeof(Program).Assembly));
    var container = new CompositionContainer(catalog);
    container.ComposeParts();
    _Container = container;
}

[Export]
public class TestHarness
{
  [Import(typeof(IEventLog))]
  public IEventLog EventLog { get; set; }

  public void RunTest()
  {
     // did not expect to need to do this, but EventLog is null if I don't

     this.EventLog.Write(some test event stuff);
  }
}
static void Main(字符串[]args)
{
InitMefContainer();
var test=_Container.GetExportedValue();
test.RunTest();
Console.ReadLine();
}
私有静态void InitMefContainer()
{
var catalog=new AggregateCatalog();
catalog.Catalogs.Add(新的AssemblyCatalog(typeof(EventLogFactory.Assembly));
catalog.Catalogs.Add(新的AssemblyCatalog(typeof(Program.Assembly));
var容器=新的合成容器(目录);
container.ComposeParts();
_容器=容器;
}
[出口]
公共类TestHarness
{
[导入(类型(IEventLog))]
公共IEventLog事件日志{get;set;}
公共无效运行测试()
{
//不希望需要这样做,但如果不这样做,EventLog将为null
this.EventLog.Write(一些测试事件内容);
}
}
这将向容器请求TestHarness实例,它将在为您实例化它时导入任何依赖项


至于保留对容器的引用,使用singleton、static、servicelocator或任何使其可用的方法,这与大多数其他IoC框架在您需要请求它们为您解析类型时的原理类似。

是的,如果对象不是由容器本身实例化的,则需要组合部分。您可以导出TestHarness,并要求容器为您提供一个实例(而不是新建),然后它将为您注入IEventLog实例