PRISM + MEF — Import & ImportMany

这一生的挚爱 提交于 2021-01-27 06:15:09

问题


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 import works, but the single import doesn't. How can I get both to work at the same time?

回答1:


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; }
}


来源:https://stackoverflow.com/questions/5171862/prism-mef-import-importmany

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!