开发者

PRISM + MEF -- Import & ImportMany

FooService.cs:

public interface IFooService
{
    int Foo();
}

[Export("Foo1", typeof(IFooService))]
public class Foo1 : IFooService
{
    public int Foo() { return 1; }
}


[Export("Foo2", typeof(IFooService))]
public class Foo2 : IFooService
{
    public int Foo() { return 2; }
}

FooViewModel.cs:

public class FooViewModel : NotificationObject
{
    [ImportMany(typeof(IFooService))]
    public IEnumerable<IFooService> FooServices { get; private set; }

    [Import("Foo1")]
    public IFooService FirstFoo { get; private set; }
}


The single import works because I have a named contract, however the multi import doesn't. If I change the Export attributes and remove the named contract, the multi i开发者_JS百科mport works, but the single import doesn't. How can I get both to work at the same time?


You can put multiple export attributes on your classes:

[Export(typeof(IFooService))]
[Export("Foo1", typeof(IFooService))]
public class Foo1 : IFooService
{
    public int Foo() { return 1; }
}

[Export(typeof(IFooService))]
[Export("Foo2", typeof(IFooService))]
public class Foo2 : IFooService
{
    public int Foo() { return 2; }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜