C# 将MEF进口与出口匹配
在下面的代码中,我试图使用MEF将导入与匹配的导出进行匹配。TestMEFClass有一个导入和一个导出,它们共享一个匹配的契约名称。导出应该在每次调用时递增一个计数器 当我将导出打印到控制台时,它没有增加计数器。有人能指出我的错误吗 多谢各位C# 将MEF进口与出口匹配,c#,.net,windows,legacy,C#,.net,Windows,Legacy,在下面的代码中,我试图使用MEF将导入与匹配的导出进行匹配。TestMEFClass有一个导入和一个导出,它们共享一个匹配的契约名称。导出应该在每次调用时递增一个计数器 当我将导出打印到控制台时,它没有增加计数器。有人能指出我的错误吗 多谢各位 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ComponentModel; using System
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.Reflection;
namespace MEFConsoleTest {
public class TestMEFClass {
/// <summary>
/// This counter should increment everytime the getter in the ExportString property gets called.
/// </summary>
private int counter = 0;
[Export("Contract_Name")]
public string ExportString {
get {
return "ExportString has been called " + counter++.ToString();
}
}
[Import("Contract_Name")]
public string ImportString { get; set; }
/// <summary>
/// Default Constructor.
/// Make a catalog from this assembly, add it to the container and compose the parts.
/// </summary>
public TestMEFClass() {
AggregateCatalog catalog = new AggregateCatalog();
catalog.Catalogs.Add(new AssemblyCatalog(Assembly.GetExecutingAssembly()));
var container = new CompositionContainer(catalog);
container.ComposeParts(this);
}
}
class Program {
static void Main(string[] args) {
TestMEFClass testClass = new TestMEFClass();
Console.WriteLine(testClass.ImportString);
Console.WriteLine(testClass.ImportString);
Console.ReadLine();
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用系统组件模型;
使用System.ComponentModel.Composition;
使用System.ComponentModel.Composition.Hosting;
运用系统反思;
名称空间MEFConsoleTest{
公共类TestMEFClass{
///
///每次调用ExportString属性中的getter时,此计数器都应递增。
///
专用整数计数器=0;
[出口(“合同名称”)]
公共字符串导出字符串{
得到{
return“ExportString已被调用”+计数器+++.ToString();
}
}
[进口(“合同名称”)]
公共字符串ImportString{get;set;}
///
///默认构造函数。
///从该部件创建目录,将其添加到容器并组成零件。
///
公共TestMEFClass(){
AggregateCatalog catalog=新的AggregateCatalog();
catalog.Catalogs.Add(新的AssemblyCatalog(Assembly.getExecutionGassembly());
var容器=新的合成容器(目录);
容器。组件(本);
}
}
班级计划{
静态void Main(字符串[]参数){
TestMEFClass testClass=新的TestMEFClass();
WriteLine(testClass.ImportString);
WriteLine(testClass.ImportString);
Console.ReadLine();
}
}
由于目前无法解释的原因,我无法让MEF和属性导入/导出在可变属性上工作。但是,使用函数成功了。我希望此代码能帮助其他人
谢谢
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.Reflection;
namespace MEFConsoleTest {
public class TestMEFClass {
/// <summary>
/// This counter should increment everytime the getter in the ExportString property gets called.
/// </summary>
private int counter = 0;
[Export("Contract_Name")]
string ExportMethod() {
return ExportString;
}
public string ExportString {
get {
return "ExportString has been called " + counter++.ToString();
}
}
[Import("Contract_Name")]
Func<string> ImportMethod;
public string ImportString { get { return ImportMethod(); } }
/// <summary>
/// Default Constructor.
/// Make a catalog from this assembly, add it to the container and compose the parts.
/// </summary>
public TestMEFClass() {
AggregateCatalog catalog = new AggregateCatalog();
catalog.Catalogs.Add(new AssemblyCatalog(Assembly.GetExecutingAssembly()));
var container = new CompositionContainer(catalog);
container.ComposeParts(this);
}
}
class Program {
static void Main(string[] args) {
TestMEFClass testClass = new TestMEFClass();
for (int x = 0; x < 10; x++) {
Console.WriteLine(testClass.ImportString);
}
Console.ReadLine();
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用系统组件模型;
使用System.ComponentModel.Composition;
使用System.ComponentModel.Composition.Hosting;
运用系统反思;
名称空间MEFConsoleTest{
公共类TestMEFClass{
///
///每次调用ExportString属性中的getter时,此计数器都应递增。
///
专用整数计数器=0;
[出口(“合同名称”)]
字符串导出方法(){
返回导出字符串;
}
公共字符串导出字符串{
得到{
return“ExportString已被调用”+计数器+++.ToString();
}
}
[进口(“合同名称”)]
Func-ImportMethod;
公共字符串ImportString{get{return ImportMethod();}}
///
///默认构造函数。
///从该部件创建目录,将其添加到容器并组成零件。
///
公共TestMEFClass(){
AggregateCatalog catalog=新的AggregateCatalog();
catalog.Catalogs.Add(新的AssemblyCatalog(Assembly.getExecutionGassembly());
var容器=新的合成容器(目录);
容器。组件(本);
}
}
班级计划{
静态void Main(字符串[]参数){
TestMEFClass testClass=新的TestMEFClass();
对于(int x=0;x<10;x++){
WriteLine(testClass.ImportString);
}
Console.ReadLine();
}
}
}