我正在使用MEF。我的申请是开放式的,但我仍然希望将其隐藏在扩展它的人之中。
e.g。 BaseAssembly有
public class ListContainer
{
[ImportMany(typeof(IBase))]
public List<IBase> MyObjects { get; set; }
public void AssembleDriverComponents()
{
.... Some code to create catalogue..
//Crete the composition container
var container = new CompositionContainer(aggregateCatalog);
// Composable parts are created here i.e. the Import and Export components assembles here
container.ComposeParts(this);
}
}
其他组件将引用基础组件。
ReferenceAssembly将有
[Export(typeof(IBase))]
public class MyDerived
{
public MyDerived()
{ }
}
我想避免在引用的程序集中派生类上的这个属性。
有可能吗?
答案 0 :(得分:2)
我认为您正在寻找的是InheritedExport
属性。您可以在IBase
界面上使用它,它会自动导出任何实现IBase
的类。
[InheritedExport(typeof(IBase))]
public interface IBase
{
// ...
}
public class MyDerived : IBase
{
// This class will be exported automatically, as if it had
// [Export(typeof(IBase))] in its attributes.
}
您可以阅读有关继承导出here的更多信息。