Вам нужен контракт (интерфейс) и контракт метаданных (интерфейс):
public interface ICarPart{
int SomeMethodFromInterface();
}
public interface ICarPartMetadata{
string /*attention to this!!!*/ NameCarPart { get; } /* Only for read. */
}
Затем вы экспортируете свои детали:
[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
}
}
Затем вы можете импортировать с ImportMany и 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;
}
}