开发者

MEF and WPF custom import definition

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


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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜