C#MEF:导出一个类型的多个对象,并导入特定的对象 [英] C# MEF: Exporting multiple objects of one type, and Importing specific ones
本文介绍了C#MEF:导出一个类型的多个对象,并导入特定的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
public class Part
{
string name;
public Part(string nm)
{
name = nm;
}
}
public class 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 //仅从Car导入SOME零件
{
[Import(typeof(Part))]
public Part engine ;
[Import(typeof(Part))]
public零件制动器;
}
有人可以解释我能如何实现这一行为吗?
解决方案
您需要合同(界面)和元数据合约(界面):
public interface ICarPart {
int SomeMethodFromInterface();
}
public interface ICarPartMetadata {
string / *注意到这个* * / NameCarPart {get; } / *仅供阅读。 * /
}
然后导出您的零件:
[Export(typeof(ICarPart))]
[ExportMetadata(NameCarPart,SteeringWheel)] / *是string! * /
public class SteeringWheel:ICarPart {
public int SomeMethodFromInterface(){
... //你的东西
}
}
[Export(typeof(ICarPart))]
[ExportMetadata(NameCarPart,Engine)] / *是string! * /
public class引擎:ICarPart {
public int SomeMethodFromInterface(){
//每个方法在每个部分都有不同的行为。
... //你的东西
}
}
[导出(typeof(ICarPart))]
[ExportMetadata(NameCarPart,Brakes)] / *是string! * /
public class制动器:ICarPart {
public int SomeMethodFromInterface(){
//每个方法在每个部分都有不同的行为。
... //你的东西
}
}
然后,您可以使用ImportMany和Lazy导入:
[ImportMany()]
IEnumerable< Lazy< ICarPart,ICarPartMetadata> > carParts = null;
public void Importing(){
...
...
foreach(carparts中的Lazy< ICarPart,ICarPartMetadata>项目){
开关(item.Metadata.ICarPartMetadata.ToString()){
caseSteeringWheel:
item.Value.SomeMethodFromInterface();
break;
caseEngine:
item.Value.SomeMethodFromInterface();
break;
caseBrakes:
item.Value.SomeMethodFromInterface();
break;
默认值:
...
break;
}
}
I have an application which exports several objects of the same Class, and plugins which import only specific ones. for example
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;
}
Could someone explain how I can achieve this behavior?
解决方案
You need a contract(interface) and a metadata contract (interface):
public interface ICarPart{
int SomeMethodFromInterface();
}
public interface ICarPartMetadata{
string /*attention to this!!!*/ NameCarPart { get; } /* Only for read. */
}
Then you export your parts:
[Export(typeof (ICarPart))]
[ExportMetadata("NameCarPart","SteeringWheel")] /* is string!! */
public class SteeringWheel : ICarPart {
public int SomeMethodFromInterface(){
... //your stuff
}
}
[Export(typeof (ICarPart))]
[ExportMetadata("NameCarPart","Engine")] /* is string!! */
public class Engine : ICarPart {
public int SomeMethodFromInterface(){
//Each method have diferent behavior in each part.
... //your stuff
}
}
[Export(typeof (ICarPart))]
[ExportMetadata("NameCarPart","Brakes")] /* is string!! */
public class Brakes : ICarPart {
public int SomeMethodFromInterface(){
//Each method have diferent behavior in each part.
... //your stuff
}
}
Then you can import with ImportMany and Lazy:
[ImportMany()]
IEnumerable<Lazy<ICarPart, ICarPartMetadata>> carParts = null;
public void Importing(){
...
...
foreach (Lazy<ICarPart,ICarPartMetadata> item in carParts){
switch (item.Metadata.ICarPartMetadata.ToString()){
case "SteeringWheel":
item.Value.SomeMethodFromInterface();
break;
case "Engine":
item.Value.SomeMethodFromInterface();
break;
case "Brakes":
item.Value.SomeMethodFromInterface();
break;
default:
...
break;
}
}
这篇关于C#MEF:导出一个类型的多个对象,并导入特定的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文