Defining custom MEF and WPF imports

I have this idea to try using my own IMPORT attribute for a new condition based class. For example, if I have:

[Import ("Position_32")] this should exist if I run a 32-bit OS, and then:

[Import ("Position_64")] if Im works with a 64-bit OS. Is there a way to make a type name for a dynamic attribute based on a condition?

Conceptually, it might look like this:

[Import (((IsWIN64 ())? Position_64 ":" Position_32 "))] This does not work because the type name must be a constant.

I want to make the new position class as transparent as possible. I mean, I used the factory method using funcs to get the desired effect, but I would like to use MEF for this. Ideas?

Extremely important

David

+4
source share
1 answer

You can use ExportMetadataAttribute like this:

[Import("Position")] [ExportMetadata("Platform", "32bit")] public YourType ... 

Then, when you go to import, use:

  [ImportMany] public Lazy<YourType,IDictionary<string,object>>[] Positions { get; set; } 

You can then check the Dictionary for the relevant metadata and use this specific platform at runtime.

In addition, you can create your own interface for highly typed metadata (instead of strings). See Export and metadata for more information .

+2
source

All Articles