C# MEF 设置属性/属性注入
有没有什么方法可以使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您必须让
CompositionContainer
为您创建一个对象才能满足其导入。将ExportAttribute
添加到IoCConstructorMef
,创建容器,然后调用container.GetExportedValue()
。You have to let the
CompositionContainer
create an object for you for its imports to be satisfied. Add anExportAttribute
toIoCConstructorMef
, create a container, and callcontainer.GetExportedValue<IoCConstructorMef>()
.你必须让 MEF 构建你的 IoCConstructorMef cm。否则导入将不会发生。
you have to let MEF construct your IoCConstructorMef cm. otherwise the import will not happen.