C# MEF 设置属性/属性注入

发布于 2024-10-01 01:53:35 字数 874 浏览 5 评论 0原文

有没有什么方法可以使用 Microsoft Extensibility Framework(MEF) 让它工作

假设我有一个接口

public interface IApplicationSettings
{
   string SettingOne { get; }
}

类,它实现了这个接口

[Export(typeof(IApplicationSettings))]
public class ApplicationSettings : IApplicationSettings
{ 
   public string SettingOne
   {
       get
       {
           return "AAA";
       }        
    }
}

类,其中包含我的接口作为属性

public class IoCConstructorMef
{
    [Import("ApplicationSettings", typeof(IApplicationSettings))]
    public IApplicationSettings ApplicationSettings { get; set; }
}

,那么我期望我的属性将被注入这里:

 static void Main(string[] args)
 {
     IoCConstructorMef cm = new IoCConstructorMef();
     //cm.ApplicationSettings - is null

 }

看起来没有在这种情况下会发生注入。

我错过了什么或者做错了什么吗?

Is there any way to get it work using Microsoft Extensibility Framework(MEF)

Lets say i have interface

public interface IApplicationSettings
{
   string SettingOne { get; }
}

Class which implementing this interface

[Export(typeof(IApplicationSettings))]
public class ApplicationSettings : IApplicationSettings
{ 
   public string SettingOne
   {
       get
       {
           return "AAA";
       }        
    }
}

Class which containg my interface as a property

public class IoCConstructorMef
{
    [Import("ApplicationSettings", typeof(IApplicationSettings))]
    public IApplicationSettings ApplicationSettings { get; set; }
}

then i am expecting that my property going to be injected here:

 static void Main(string[] args)
 {
     IoCConstructorMef cm = new IoCConstructorMef();
     //cm.ApplicationSettings - is null

 }

Looks like there no injection happens in this case.

Am i missed something or did something wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

电影里的梦 2024-10-08 01:53:35

您必须让CompositionContainer为您创建一个对象才能满足其导入。将 ExportAttribute 添加到 IoCConstructorMef,创建容器,然后调用 container.GetExportedValue()

You have to let the CompositionContainer create an object for you for its imports to be satisfied. Add an ExportAttribute to IoCConstructorMef, create a container, and call container.GetExportedValue<IoCConstructorMef>().

彼岸花ソ最美的依靠 2024-10-08 01:53:35

你必须让 MEF 构建你的 IoCConstructorMef cm。否则导入将不会发生。

you have to let MEF construct your IoCConstructorMef cm. otherwise the import will not happen.

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