Custom MEF attribute with metadata

Apr 18, 2010 at 6:58 PM

Using custom attribute with typed metadata return no result if the

Custom attribute does not assign with

//[AttributeUsage (AttributeTargets.Class)]

Strangely untyped metadata does work

You can download code sample that demonstrate this behavior from

http://cid-9bf7c1a515d76a9a.skydrive.live.com/self.aspx/Code%20Samples/MEF/Temp/MEFTypedMetadata.zip

 

does it a bug?

 

Apr 18, 2010 at 9:27 PM
It is not a bug. What you are seeing is because by default export has the AllowMultiple property of the attribute usage attribute set to true. When we see this we expect that the metadata view has array members as there might be multiple sets of metadata. When you override AttributeUsage like you have done, AllowMultiple defaults back to false. If you set it to true explicitly you will see again that the exports will not be found. Also if you change the metadata view types to be array you will see that it works the old way.

In general you should override attribute usage to set AllowMultiple to false unless you expect multiple sets of metadata / multiple exports on the member.

An example of where you would want to leave AllowMultiple to true would be if you have a custom Category metadata attribute that allows one or more categories. In this case the metadata view Category prop would be an array.

Glenn