MEF field imports not being resolved
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;
[I开发者_运维问答mportingConstructor]
public TestModule(IMyDataService myDataService)
{
_myDataService = myDataService;
}
}
[PartCreationPolicy(CreationPolicy.Shared)]
[Export(typeof(IMyDataService))]
public class MyDataService : IMyDataService
{
}
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.
Change the access modifier from private to public and check if that works.
精彩评论