2010-10-26 96 views
2

我有MEF/Prism 4項目,我可以通過ImportingConstructor解析導入,但不能通過同一類中的字段導入。MEF字段導入未解決

在下面的示例代碼中,myDataService在構造函數中正確解析。但_myDataServiceFieldImport不解決,儘管Import屬性。相同的結果,無論它是一個領域或財產。

任何明顯的我在這裏失蹤?

[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 
{ 

} 

回答

3

原來,這只是我愚蠢 - 我正在檢查構造函數中的屬性/字段值,而它們只有在構造函數完成後纔會被解析。

+0

我剛剛做到了! – sambomartin 2014-07-02 16:04:27

0

更改訪問修飾符從私人到公共,並檢查是否有效。

+0

不,不是這樣 - 正如我所說:'無論是田地還是財產,結果都是一樣的。 – Marcus 2010-10-26 09:45:33

+0

我剛剛嘗試導入到私有字段,並且我得到FieldAccessException,或者如果它是一個屬性,則爲MethodAccessException。 – Fermin 2010-10-26 10:00:18

+0

這是什麼環境? Silverlight,桌面,ASP.NET等? – 2010-10-26 19:45:26