Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ms-access/4.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

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

C# 使用带MEF的类型导出和导入委托

C# 使用带MEF的类型导出和导入委托,c#,delegates,mef,C#,Delegates,Mef,我们目前正在使用MEF按名称导入和导出代理 public delegate IThing ThingGenerator(); [Export("ThingGenerator")] public IThing MyThingGenerator() { ... } [ImportMany("ThingGenerator")] public IEnumerable<ThingGenerator> ThingGenerators { get; set; } public委托thing

我们目前正在使用MEF按名称导入和导出代理

public delegate IThing ThingGenerator();

[Export("ThingGenerator")]
public IThing MyThingGenerator() { ... }

[ImportMany("ThingGenerator")]
public IEnumerable<ThingGenerator> ThingGenerators { get; set; }
public委托thing ThingGenerator();
[导出(“ThingGenerator”)]
公共IThing MyThingGenerator(){…}
[进口数量(“ThingGenerator”)]
公共IEnumerable ThingGenerators{get;set;}
对于可维护性,我们希望使用类型导入和导出。然而,我们似乎无法做到这一点

[Export(typeof(ThingGenerator))]
public IThing MyThingGenerator() { ... }

[ImportMany(typeof(ThingGenerator))]
public IEnumerable<ThingGenerator> ThingGenerators { get; set; }
[导出(类型(ThingGenerator))]
公共IThing MyThingGenerator(){…}
[进口数量(类型(ThingGenerator))]
公共IEnumerable ThingGenerators{get;set;}

在某些表单中,我们会收到错误,说明导出的函数与委托类型不匹配。在其他场景中,类型似乎不会被导出。有人成功地做到了吗?

您实际上可以按类型导入和导出,只有在您的情况下,您应该尝试导出和导入一个typeof(IThing)而不是typeof(ThingGenerator)

通常,导出的零件实现一些接口(可以是空接口,其作用类似于标记),因此您可以识别要导入的内容,但它也适用于类型

这是一个很好的视频教程,可以帮助您开始使用MEF(它在SilverLight中,但也适用于WPF,也适用于Winforms)

检查第2部分,当他使用元数据创建自定义导出属性时,它可能特别有用


希望这对您有所帮助,因为您实际上可以按类型导入和导出,只有在您的情况下,您应该尝试导出和导入typeof(IThing)而不是typeof(ThingGenerator)

通常,导出的零件实现一些接口(可以是空接口,其作用类似于标记),因此您可以识别要导入的内容,但它也适用于类型

这是一个很好的视频教程,可以帮助您开始使用MEF(它在SilverLight中,但也适用于WPF,也适用于Winforms)

检查第2部分,当他使用元数据创建自定义导出属性时,它可能特别有用


希望这有帮助,我同意你的看法。另一种方法是使用Generate方法使ThingGeneratorFactory类似于类。我同意你的观点。另一种方法是使用Generate方法使ThingGenerator工厂类似于类。