在.NET 4.0的MEF中使用自定義屬性類時,我得到了一個CompositionContractMistachException
。MEF自定義屬性類需要ctor(IDictionary <,>)
Unable to create an instance of the Metadata view '(snip).ModuleExportAttribute, (snip), Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' because a constructor could not be selected. Ensure that the type implements a constructor which takes an argument of type IDictionary<string, object>.
這裏是我ModuleExportAttribute
類,沒有什麼特別的:
[MetadataAttribute]
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = false)]
public sealed class ModuleExportAttribute : ExportAttribute
{
public ModuleExportAttribute(string name) : base(typeof(IModule))
{
this.Name = name;
}
public string Name { get; private set; }
}
用法是這樣的:
[ModuleExport("MyModule")]
public class MyModule : IModule
{
...
}
添加問道構造後,異常消失。
但是,我找不到任何提及此要求的參考。相反,我看到許多使用自定義屬性類的例子和博客帖子,沒有這樣的構造函數。我在這裏錯過了什麼嗎?