简体   繁体   中英

Can MEF Export/Import static classes?

Is it possible to do something like:

[Export(typeof(Settings)]
public static class Settings
{
   public string Name {get;set;}
   public string Color {get;set;}
}

[Import(typeof(Settings)]
Settings s;

You can't export a static class, as there is no instance to wire up. The second line:

Settings s;

Would be a compiler error, as you can't instantiate a static class.

That being said, this is really not normally necessary. MEF will automatically create a single instance (by default) of your Settings class (provided it's not static), and set that same instance to any Import specified. This effectively gives you a "singleton-like" class, without any of the downsides of using a static class or a singleton.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM