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?
您可以在类上放置多个导出属性:
[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; }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)