Reputation: 51
I have this idea to try to use a custom IMPORT attribute to new up a class based on a condition. For example, if I have:
[Import("Position_32")] this should exist if I'm running a 32bit OS, and then:
[Import("Position_64")] if Im running a 64 bit OS. Is there any way to make the type name for the attribute dynamic based on a condition?
Conceptually it could look like this:
[Import(((IsWIN64()) ? "Position_64" : "Position_32"))] This doesn't work because the type name needs to be a constant.
I want to make the newing up of the proper position class as transparent as possible. I mean I did a factory based method using funcs to get my desired effect but I'd love to use MEF for this. Ideas?
THanks much,
David
Upvotes: 4
Views: 461
Reputation: 564901
You could use ExportMetadataAttribute like so:
[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 appropriate metadata, and use that specific platform, at runtime.
In addition, you can make a custom interface for strongly typed metadata (instead of strings). For details, see Exports and Metadata.
Upvotes: 2