VS 扩展中具有 [Export] 的 MEF 组件 - 未找到与约束匹配的导出
MEF component with [Export] in VS extension - No exports found that match the constraint
在 Visual Studio 扩展中,我正在尝试导出 MEF 组件并稍后找到它。
我有这个class:
[Export(typeof(IBoilerplateSettings))]
public class BoilerplateSettings : IBoilerplateSettings
{
...
此代码在 OleMenuCommand 的回调中解析。从 this pull request.
中获取示例
var componentModel = (IComponentModel)this.package.GetService(typeof(SComponentModel));
var settings = componentModel.DefaultExportProvider.GetExportedValue<IBoilerplateSettings>();
但它每次都会抛出这个错误:
No exports were found that match the constraint:
ContractName UnitTestBoilerplate.IBoilerplateSettings
RequiredTypeIdentity UnitTestBoilerplate.IBoilerplateSettings
两者都在同一个程序集中。我已经在 System.Composition
和 System.ComponentModel.Composition
中尝试了 [Export]
属性,但都不起作用。
我正在查看 the docs for MEF in VS,但听起来添加属性应该就可以了。我也尝试过清除 ComponentModel 缓存,但没有用。我在这里错过了什么?
通常要检查两件事:
- 确保您的项目在 .vsixmanifest 中被提及为 MEF 组件。如果不是,VS 将不会查看您的程序集。
- 如果您使用的是 Visual Studio 2015 或更高版本,在 ComponentModelCache 文件夹中有一个 .err 文件,其中列出了创建 MEF 组合时的错误。确保你关心的东西不在那里。 (里面有垃圾是正常的......我们并不总是最擅长以干净的状态运送垃圾。)
如果这不起作用,通常有一种激进的方法,即在 Visual Studio 为 运行 时中断所有异常,然后查看是否可以找到与您的扩展相关的任何内容.您可能会发现其他一些加载异常或其他根本原因。不幸的是,这是一种非常分散的方法。
谢谢杰森·马林诺夫斯基。
这很重要。如果你想在 vsix 中使用 MEF。
需要注意vsixmainfest中的resource是否有对应的MefComponent
在 Visual Studio 扩展中,我正在尝试导出 MEF 组件并稍后找到它。
我有这个class:
[Export(typeof(IBoilerplateSettings))]
public class BoilerplateSettings : IBoilerplateSettings
{
...
此代码在 OleMenuCommand 的回调中解析。从 this pull request.
中获取示例var componentModel = (IComponentModel)this.package.GetService(typeof(SComponentModel));
var settings = componentModel.DefaultExportProvider.GetExportedValue<IBoilerplateSettings>();
但它每次都会抛出这个错误:
No exports were found that match the constraint:
ContractName UnitTestBoilerplate.IBoilerplateSettings
RequiredTypeIdentity UnitTestBoilerplate.IBoilerplateSettings
两者都在同一个程序集中。我已经在 System.Composition
和 System.ComponentModel.Composition
中尝试了 [Export]
属性,但都不起作用。
我正在查看 the docs for MEF in VS,但听起来添加属性应该就可以了。我也尝试过清除 ComponentModel 缓存,但没有用。我在这里错过了什么?
通常要检查两件事:
- 确保您的项目在 .vsixmanifest 中被提及为 MEF 组件。如果不是,VS 将不会查看您的程序集。
- 如果您使用的是 Visual Studio 2015 或更高版本,在 ComponentModelCache 文件夹中有一个 .err 文件,其中列出了创建 MEF 组合时的错误。确保你关心的东西不在那里。 (里面有垃圾是正常的......我们并不总是最擅长以干净的状态运送垃圾。)
如果这不起作用,通常有一种激进的方法,即在 Visual Studio 为 运行 时中断所有异常,然后查看是否可以找到与您的扩展相关的任何内容.您可能会发现其他一些加载异常或其他根本原因。不幸的是,这是一种非常分散的方法。
谢谢杰森·马林诺夫斯基。 这很重要。如果你想在 vsix 中使用 MEF。 需要注意vsixmainfest中的resource是否有对应的MefComponent