开发者

C# MEF Set Property/Property Injection

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(stri开发者_如何学运维ng[] 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?


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>().


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜