MEF 中具有元数据访问的多个接口?
我创建了三个接口:
interface A
{
B obj{ get; }
}
interface B
{
C obj { get; }
}
interface C { }
[Export(typeof(C))]
class class C1 : C
{
//.........
}
[Export[typeof(B)]
[ExportMetadata("Name", "ABC")
class class1 : B
{
[Import(typeof(C))]
public C Cvalues;
public C obj
{
get { return Cvalues; }
}
}
[Export(typeof(B))]
[ExportMetadata("Name", "XYZ")]
class class2 : B
{
[Import(typeof(C))]
public C Cvalues;
public C obj { get { return Cvalues; } }
}
现在,我想要在接口 A 中导出接口 B 的元数据
[Export(typeof(A))
class AA : A
{
// how i get the B here
[Import(typeof(B)]
public B Bvalues;
public B objB { get { return Bvalues; } }
}
如果我尝试在接口 A 中创建接口 B 的属性,那么在一次导入 B 时,它会给我输出,但是 ImportMany 怎么样,因为接口 B 返回 ImportMany ...
请告诉我如何在接口 A 中获取所有接口 B 导出。
I have created three interfaces:
interface A
{
B obj{ get; }
}
interface B
{
C obj { get; }
}
interface C { }
[Export(typeof(C))]
class class C1 : C
{
//.........
}
[Export[typeof(B)]
[ExportMetadata("Name", "ABC")
class class1 : B
{
[Import(typeof(C))]
public C Cvalues;
public C obj
{
get { return Cvalues; }
}
}
[Export(typeof(B))]
[ExportMetadata("Name", "XYZ")]
class class2 : B
{
[Import(typeof(C))]
public C Cvalues;
public C obj { get { return Cvalues; } }
}
Now, I want Export metadata of interface B in interface A
[Export(typeof(A))
class AA : A
{
// how i get the B here
[Import(typeof(B)]
public B Bvalues;
public B objB { get { return Bvalues; } }
}
If i try to create the property of interface B in interface A then at one import of B it's give me output but what about ImportMany because interface B returns ImportMany...
Please give me some idea how can I get my all interface B exports in interface A.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
可以使用
Lazy
实例导入元数据。简单来说,这意味着:但是,定义元数据契约通常是个好主意,例如:
您可以使用它来代替字典:
使用元数据契约将使您能够使用静态类型的接口实现,MEF 将自动为您项目。
Metadata can be imported using a
Lazy<T, TMetdat>
instance. In simple terms this means:But, often it is a good idea to define a metadata contract, e.g.:
Which you can use instead of the dictionary:
Using metadata contracts will enable you to use a statically-typed interface implementation, that MEF will automatically project for you.