tags:

views:

22

answers:

2

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 
{

}
A: 

Change the access modifier from private to public and check if that works.

Fermin
No that's not it - as I said: 'Same result whether it's a field or property.'
Marcus
I've just tried to import to a private field and I get FieldAccessException, or a MethodAccessException if it's a property.
Fermin
What environment is this running in? Silverlight, Desktop, ASP.NET, etc?
Daniel Plaisted
A: 

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.

Marcus