gpt4 book ai didi

c# - PRISM + MEF -- 导入 & ImportMany

转载 作者:太空狗 更新时间:2023-10-29 23:34:29 26 4
gpt4 key购买 nike

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


单一导入有效,因为我有一个命名契约(Contract),但多重导入没有。如果我更改导出属性并删除命名契约(Contract),则多导入有效,但单导入无效。如何让两者同时工作?

最佳答案

您可以在您的类上放置多个导出属性:

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

关于c# - PRISM + MEF -- 导入 & ImportMany,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5171862/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com