The composition produced a single composition error, with 2 root causes. The root causes are provided below. Review the CompositionException.Errors property for more detailed information.
1) No exports were found that match the constraint:
ContractName MyNonMefInterface
RequiredTypeIdentity MyNonMefInterface
Resulting in: Cannot set import 'MyMefClass..ctor (Parameter="myNonMefClass", ContractName="MyNonMefInterface")' on part 'MyMefClass'.
反正有告訴MEF到不嘗試導入「MyInterface的」?它如果不通過它的構造函數被拖欠已可選PARAM:
public MyMefClass() : (new MyNonMefClass()) {}
public MyMefClass(myNonMefInterface myNonMefClass) {
_myNonMefClass = myNonMefClass;
}
我使用MEF 2的新的屬性更少的配置,做我的RegistrationBuilder像這樣:
var builder = new RegistrationBuilder();
builder.ForTypesDerivedFrom<IMyMefClass>().ExportInterfaces();
...
var aggregateCatalog = new AggregateCatalog();
aggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(MyMefClass).Assembly, builder));
var container = new CompositionContainer(aggregateCatalog, CompositionOptions.DisableSilentrejection);
基本上,我總是將MyNonMefClass的默認設置設置爲默認構造函數,但允許我自己將它重載進行測試。它沒有設置爲MEF和來自另一個DLL,所以如果我可以告訴MEF忽略它,並使用不帶params的構造函數,那將是最簡單的解決方案。任何人都知道如何做到這一點?
編輯:我能夠通過在空的構造函數上設置[ImportingConstructor]
標記來解決它,但這意味着我必須在該項目中引用MEF,我是否可以通過新的流利API使用RegistrationBuilder改爲?
你從哪裏得到.SelectConstructor和ctors.Min和ctor。 GetParameter?我沒有它... –
@GutembergRibeiro是否使用MEF 2或MEF 1?http://blogs.msdn.com/b/bclteam/archive/2011/11/01/getting-started-with -convention-based-part-registration-in-mef-version-2.aspx有更多的細節,我也將它添加到我的文章 – John
奇怪的想法是因爲我根本沒有構造器,所以它意味着將會調用默認的無參數ctor,但是Idk爲什麼仍然會給出以下錯誤: 構圖產生了單個構圖錯誤。根源在下面提供。查看CompositionException.Errors屬性以獲取更多詳細信息。 1)未找到方法:'Void PI.Switch.Hosting.AppBase..ctor(System.String)'。 –