导入类而不导出它

时间:2012-12-12 07:38:34

标签: .net dependencies mef composition

我正在使用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()
    { }
}

我想避免在引用的程序集中派生类上的这个属性。

有可能吗?

1 个答案:

答案 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的更多信息。