简体   繁体   中英

MEF Filter ImportMany

Let's say that I have the following 3 classes:

[Export("Settings",typeof(ISettingsItem)]
[PartCreationPolicy(CreationPolicy.Shared)]
public class Settings1Vm {}

[Export("Settings",typeof(ISettingsItem)]
[PartCreationPolicy(CreationPolicy.Shared)]
public class Settings2Vm {}

[Export("Settings",typeof(ISettingsItem)]
[PartCreationPolicy(CreationPolicy.Shared)]
public class Settings3Vm {}

If is possible for example to do this:

[Import]
private ISettingsItem SettingEntry{ get; set; }

But in a way that this import only takes the export of the Settings2VM class?

Regards

It seems that the solution was:

[Export("Settings",typeof(ISettingsItem)]
[Export("Settings2Vm ",typeof(ISettingsItem)]
[PartCreationPolicy(CreationPolicy.Shared)]
public class Settings2Vm {}

[Import("Settings2Vm ", typeof(ISettingsItemVM))]
private Settings2Vm SettingEntry{ get; set; }

This way I will always have the same instance.

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