This project has moved and is read-only. For the latest updates, please go here.

Convention-based export with attribute-based metadata

Dec 1, 2012 at 1:11 PM

Hello, I'd like to use RegistrationBuilder to export all derived types:


I need to have an ability to apply metadata individually at class level:

    [ExportMetadata("Name", "boo")]
    public class Foo1 : IFoo

The way of importing parts is:

    public IEnumerable<Lazy<IFoo, IFooMeta>> foos;

No parts are discovered, because of lack of metadata. I've read how attributes overwrite convention-based info - link. However, it'd require adding an explicit ExportAttribute to each "Foo" class as well. Then, there is no point in having a RegistrationBuilder.

While I seem to understand the reasoning behind this design, my needs definitely fit in a convention - all classes implementing IFoo should be exported with their metadata. One work-around I can think of is:


rb.ForTypesDerivedFrom<IFoo>().Export<IFoo>().AddMetadata("Name", retrieveMetaFunc);

where retrieveMetaFunc is a custom method that extracts required metadata from a type. I'm in search for a cleaner, built-in approach that'd eliminate the need for repetitive ExportAttributes on every class definition. For the sake of example something like this:

Notice I'm not able to use custom export attributes, because I have multiple IFooMeta on each exported part.