问题
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