gpt4 book ai didi

C# MEF : Exporting multiple objects of one type, 和导入特定的

转载 作者:行者123 更新时间:2023-11-30 13:20:27 25 4
gpt4 key购买 nike

我有一个应用程序导出同一个类的多个对象,以及只导入特定对象的插件。例如

public class Part
{
string name;
public Part(string nm)
{
name = nm;
}
}

public class Car //Exports ALL Parts of the Car
{
[Export(typeof(Part))]
public Part steeringWheel = new Part("SteeringWheel");

[Export(typeof(Part))]
public Part engine = new Part("Engine");

[Export(typeof(Part))]
public Part brakes = new Part("Brakes");
}

public class SystemMonitorPlugin //Imports only SOME Parts from the Car
{
[Import(typeof(Part))]
public Part engine;

[Import(typeof(Part))]
public Part brakes;
}

有人可以解释一下我如何才能实现这种行为吗?

最佳答案

您可以命名导出:

[Export("SteeringWheel", typeof(Part))]

当你想要一个特定的,

[Import("Engine", typeof(Part))]

如果不指定名称,您仍然可以导入许多类型的零件。

关于C# MEF : Exporting multiple objects of one type, 和导入特定的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7981504/

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