Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/318.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#_Ioc Container_Mef - Fatal编程技术网

C# 跟踪MEF容器创建的实例

C# 跟踪MEF容器创建的实例,c#,ioc-container,mef,C#,Ioc Container,Mef,我正在使用MEF创建同一导出的多个实例 我想跟踪我创建的实例,或者查询容器,或者使用重新编译的集合,但是我从来没有得到我的实例 代码如下: interface IFoo{}; [Export(typeof(IFoo)),PartCreationPolicy(CreationPolicy.NonShared)] class Foo{}; class FooTracker{ CompositionContainer _container; int HowManyValue

我正在使用MEF创建同一导出的多个实例

我想跟踪我创建的实例,或者查询容器,或者使用重新编译的集合,但是我从来没有得到我的实例

代码如下:

interface IFoo{};
[Export(typeof(IFoo)),PartCreationPolicy(CreationPolicy.NonShared)]
class Foo{};

  class FooTracker{
     CompositionContainer _container; 
     int HowManyValues()
     {
       // this always returns 1 and invokes a constructor
       return _container.GetExportedValue<IFoo>().Count();
     }

     int HowManyExports(){
       // idem
       return _container.GetExports<IFoo>().Count();
     }

     // idem
     [ImportMany(AllowRecomposition=true,AllowRecomposition=true)]
     protected IEnumerable<IFoo> Foos { get; set; }
  }
接口IFoo{};
[导出(类型(IFoo)),PartCreationPolicy(CreationPolicy.NonShared)]
类Foo{};
职业足球运动员{
合成容器_容器;
int howmanyvalue()
{
//这总是返回1并调用构造函数
返回_container.GetExportedValue().Count();
}
int HowManyExports(){
//同上
返回_container.GetExports().Count();
}
//同上
[ImportMany(AllowRecomposition=true,AllowRecomposition=true)]
受保护的IEnumerable Foos{get;set;}
}
我希望得到的是已经存在的实例,而不是创建一个新的实例(如果没有)

谢谢,
Florian

MEF不提供此功能。您可以让每个IFoo实现导入一个IFooTracker,并在IFooTracker中调用一个方法来注册IFoo。

MEF不提供此功能。您可以让每个IFoo实现导入一个IFooTracker,并在IFooTracker中调用一个方法来注册IFoo。

+1叮当。使您的IFooTracker保持静态,并且可以使用IDisposable来清理注册,但这是您跟踪任何实例的方式。+1叮叮。使您的IFooTracker保持静态,并且可以IDisposable来清理注册,但这是跟踪任何实例的方式。