MEF 字段导入未解决

发布于 2024-09-29 06:19:52 字数 718 浏览 0 评论 0原文

我有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

森罗 2024-10-06 06:19:53

事实证明,这只是我的愚蠢——我正在检查构造函数中的属性/字段值,而它们只有在构造函数完成后才会被解析。

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.

智商已欠费 2024-10-06 06:19:53

将访问修饰符从 private 更改为 public 并检查是否有效。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文