我正在开发一个开放式应用程序,我是 MEF 的新手。我需要从派生类中完全隐藏 MEF。所以这是我的场景。
我有一个 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);
}
}
[InheritedExport(typeof(IBase))]
public abstract class Base : IBase
{
private IInfoBase infoBase;
//This is something which I want to do. If I have a derived class from Base.
Then It does not need to use ImportingConstructor.
[ImportingConstructor()]
public Base(InfoBase nfoBase)
{
this.infoBase = infoBase;
}
}
[InheritedExport(typeof(IInfoBase))]
public interface IInfoBase
{
string Category { get; set; }
}
public class InfoBase : IInfoBase
{
public string Category
{
get;
set;
}
}
其他组件将参考基础组件。
ReferenceAssembly 将有
public class Derived : Base
{
public Derived(BaseInfo info)
: base(info)
{
info.Category = "CategoryA";
}
}
在这种情况下,MEF 不会为派生对象创建对象。
总之,我还需要像 InheritedExport 这样的 ImportingConstructor 。