2010-09-06 106 views
1

我的问题标题听起来有点难 - 对不起。我是MEF的新成员:-)。MEF:我如何导入一个具有构造函数的类,该构造函数标有ImportingConstructorFlag

我的情景:

public class MainClass 
{ 
    [ImportMany(typeof(ITest))] 
    private List<ITest> Tests { get; set; } 

    public MainClass() 
    { 
    Init(); 
    } 

    private void Init() 
    { 
    DirectoryCatalog catalog = new DirectoryCatalog(@"./"); 
    CompositionContainer container = new CompositionContainer(catalog); 
    container.ComposeParts(this); 
    } 
} 



[Export("BirthdayJob")] 
[Export(typeof(ITest))] 
public partial class BirthdayTest : ITest 
{ 
    [ImportingConstructor] 
    public BirthdayUserControl(IParameter parameter) 
    { 
    InitializeComponent(); 
    this.Parameter = jobParameter; 
    } 

    public IParameter Parameter { get; set; } 
} 

[Export(typeof(IParameter))] 
[Export("BirthdayParameter")] 
public class BirthdayJobParameter : IParameter 
{ 
    public override string ToString() 
    { 
     return "Birthday Remember"; 
    } 
} 


public interface IParameter : IMefInterface 
{ 

} 

public interface IMefInterface 
{ 

} 

在测试的泛型列表,我应该有相关的IParameter对象的所有可能的ITEST对象。不幸的是,通用列表中没有任何项目。

你能帮忙吗?我做错了什么?

在此先感谢。

问候,亲

//编辑

所以,我对于我的问题编译的类:

using System; 
using System.ComponentModel.Composition; 
using System.ComponentModel.Composition.Hosting; 

namespace ObjectContracts 
{ 
    public class Program 
    { 
     static void Main(string[] args) 
     { 
      var container = new CompositionContainer(new TypeCatalog(typeof (IFoo), typeof (Bar), typeof(Foo))); 
      var bar = container.GetExportedValue<Bar>(); 
      Console.WriteLine(bar.Foo.Message); 
      Console.ReadLine(); 

     } 

    } 

    [InheritedExport] 
    public interface IFoo 
    { 
     string Message { get; set; } 
    } 

    [Export] 
    public class Bar 
    { 
     [ImportingConstructor] 
     public Bar([Import("Foo")]IFoo foo) 
     { 
      this.Foo = foo; 
     } 

     public IFoo Foo { get; set;} 
    } 

    [Export("Foo")] 
    public class Foo : IFoo 
    { 
     public Foo() 
     { 
      Message = ":-)"; 
     } 
     public string Message { get; set; } 
    } 
} 

我该怎么办错了吗?请帮我:-)

问候,帕特里克

+0

你会得到什么?如果有的话....你知道MEFX组成分析工具(http://msdn.microsoft.com/en-us/library/ff576068.aspx),它可以帮助你解决这种情况吗? – 2010-09-06 16:21:44

+0

感谢您的建议。但所有部件都正确装载。只有属性不会被正确注入... :-( – bitsmuggler 2010-09-07 06:20:30

回答

2

的一点是,如果您在使用出口合同(你的情况“BirthdayJob”),你需要指定你的进口,以及。像这样:

[Export("BirthdayJob",typeof(ITest))] 
public partial class BirthdayTest : ITest 
{ 
    // class definition 
} 

而且你的导入:

public class MainClass 
{ 
    [ImportMany("BirthdayJob",typeof(ITest))] 
    private List<ITest> Tests { get; set; } 

    // class definition 
} 

关于合约的伟大的事情是,他们让你为特定类型的特定对象的实例组和滤除对象的任何不需要的情况下,一个特定的类型。

MEF是最酷的!

[Export("PeopleLivingInEdmontonAlbertaCanada",typeof(IPerson))] 
public Person KevinParkinson { get; set; } 
0

你随时随地出口IParameter?在你发布的代码中,你没有,这就是为什么生日测试没有被拿起。

+0

是的,他是!一路向下滚动代码 - 类'BirthdayJobParameter'明确导出'IParameter' – 2010-09-06 16:45:36

+0

@marc_s糟糕,我一定错过了滚动条!:) – 2010-09-07 02:58:43

1

瞎猜,这是可能的工作目录中DirectoryCatalog(取决于你如何运行的应用程序。)

为了验证这是否是问题,请更换:

DirectoryCatalog catalog = new DirectoryCatalog(@"./"); 

随着或者:

DirectoryCatalog catalog = new DirectoryCatalog(@"<full path to directory>"); 

或:

AssemblyCatalog catalog = new AssemblyCatalog(typeof(BirthdayTest).Assembly); 
+0

感谢您的回答,但当前目录(@“./”)是正确的。我已经用调试器验证过了。在ComposeParts()调用之后,所有程序集都被正确加载,并且所有部分都被更正列出... – bitsmuggler 2010-09-07 06:19:33

+0

不客气 - 我虽然没有想法。如果你可以发布一个完整的(可编译的)例子,我可以看看。 – 2010-09-11 09:34:50

1

我找到了解决方案。在foo类的导出类中应该是派生接口的引用。具有导入构造函数标志的构造函数也应该具有对该接口的引用。

[Export] 
    public class Bar 
    { 
     [ImportingConstructor] 
     public Bar([Import("Foo", typeof(IFoo))]IFoo foo) 
     //public Bar([Import(typeof(IFoo))]IFoo foo) 
     { 
      this.Foo = foo; 
     } 

     public IFoo Foo { get; set;} 
    } 

    [Export("Foo", typeof(IFoo))] 
    public class Foo : IFoo 
    { 
     public Foo() 
     { 
      Message = ":-)"; 
     } 
     public string Message { get; set; } 
    } 
相关问题