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
{
}
原来只是我很笨-我在检查构造函数中的属性/字段值,而构造函数完成后才可以解析它们。
将访问修饰符从私有更改为公共,然后检查是否可行。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.