Unexpected behavior when using multiple metadata along with InheritedExport attribute

Jan 29, 2010 at 12:14 PM

Unexpected behavior when using multiple metadata along with InheritedExport attribute

As you can see in the linked snippet:

http://codepaste.net/jnheqa

whenever you expose the export part using inherited export attribute instead of direct export decoration,

you will get no result if your import is decorated for getting metadata.

Does it have any explanation?

Developer
Feb 1, 2010 at 3:23 PM

OK the simple answer is metadata is never inherited even if you use InheritedExport. The rule for metadata is that for any given ExportAttribute (or normal, custom, or InheritedExport) the only metadata that gets associated with that export is all the metadata attributes that are applied to the exact same member/type, we will not search up or down the hierarchy to find metadata.

While this may sound sort of limiting it was an explicit design decision because of the focus we intended for InheritedExport and the complex nature of trying to develop inheritance rules for metadata.

InheritedExportAttribute is intended to be used in legacy systems or systems where the author wants to hide the details of MEF from the extension authors. In either case the extension authors would not have to directly use MEF and thus wouldn't be putting any MEF attributes on their types, including metadata attributes. If however the extension authors are required to place metadata attributes on their extensions then our suggestion would be to provide them with a custom export attribute that is also a custom metadata attribute. In which case the extension author would only have to apply one attribute which would have required metadata values as constructor arguments.

Hope that makes sense,
Wes

Feb 3, 2010 at 1:42 PM

You’re right about the custom attribute in cases that the metadata does not allow multi,

otherwise metadata cannot inherit the ExportAttribute.

This was the case that I was looking for, when I tried to use the InheritedExport solution.

The motivation for that solution was that I want my consumer to have as little space for errors as possible, and to put only the decoration they must.

For the newer MEF versions you may consider to enable property on the metadata attribute definition  which will explicitly define the type that it related to,

Something like [MeatdataAtribute (typeof(IFoo))]

This way MEF won’t have to deal with inheritance of metadata and the consumer can add metadata for InheritanceExport,

As for the InheritanceExport scenario, the feedback that I get from the real world is that people like to inherit the interface without any additional decoration, so the inheritance will become their export.