MEF 字段导入未解决
我有 MEF/Prism 4 项目,我可以通过 ImportingConstructor 解析导入,但不能通过同一类中的字段导入。
在下面的示例代码中,myDataService
在构造函数中正确解析。但是,尽管有 Import
属性,但 _myDataServiceFieldImport
并未解析。无论是字段还是属性,结果都是相同的。
我在这里缺少什么明显的东西吗?
[ModuleExport(typeof(TestModule))]
public class TestModule : IModule
{
private IMyDataService _myDataService;
[Import]
private IMyDataService _myDataServiceFieldImport;
[ImportingConstructor]
public TestModule(IMyDataService myDataService)
{
_myDataService = myDataService;
}
}
[PartCreationPolicy(CreationPolicy.Shared)]
[Export(typeof(IMyDataService))]
public class MyDataService : IMyDataService
{
}
I have MEF/Prism 4 project for which I can resolve imports via the ImportingConstructor, but not via field imports in the same class.
In the sample code below, myDataService
is correctly resolved in the constructor. But _myDataServiceFieldImport
isn't resolved, despite the Import
attribute. Same result whether it's a field or property.
Anything obvious I'm missing here?
[ModuleExport(typeof(TestModule))]
public class TestModule : IModule
{
private IMyDataService _myDataService;
[Import]
private IMyDataService _myDataServiceFieldImport;
[ImportingConstructor]
public TestModule(IMyDataService myDataService)
{
_myDataService = myDataService;
}
}
[PartCreationPolicy(CreationPolicy.Shared)]
[Export(typeof(IMyDataService))]
public class MyDataService : IMyDataService
{
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
事实证明,这只是我的愚蠢——我正在检查构造函数中的属性/字段值,而它们只有在构造函数完成后才会被解析。
Turns out it was just me being dumb - I was checking the property/field values in the constructor, whereas they're only ever going to be resolved once the constructor has completed.
将访问修饰符从 private 更改为 public 并检查是否有效。
Change the access modifier from private to public and check if that works.