Generating export metadata based upon type with ConventionBuilder

Jul 1, 2012 at 9:43 PM
Edited Jul 1, 2012 at 9:45 PM

I have been investigating the use of the MEF ConventionBuilder for convention based discovery of pages for a MVVM framework I am developing (NB: ConventionBuilder is the equivalent to RegistrationBuilder when using the version of MEF for Win8 Metro-style apps). What I would like to achieve is to apply a specific export to all types that end "Page". This can be done by,

conventionBuilder.ForTypesMatching(type => type.Name.EndsWith("Page"))
    .Export((builder) => builder.AsContractType<object>()
                                .AsContractName("CocoonPage")
                                .AddMetadata("PageName", "Home"))

However rather than exporting all the types with the page name set as "Home" I would like to use the start of the type name (i.e. a type called "AboutPage" with the page name attribute set as "About"). Unfortunately the Export extension method does not supply any overrides that provide the type name.

Is there any way I can achieve my aim or am I approaching this from the wrong angle?

Thanks,

    Andy

NB: In contrast I could add this to part metadata with the following code...

.AddPartMetadata("PageName", type => type.Name.Substring(0, type.Name.Length - 4))
Jul 2, 2012 at 5:35 PM

Hi Andy,

There's a second overload of AddMetadata that should do what you need. I don't think it was added recently, but perhaps try updating the package with NuGet?

This compiles for me when substituted into your example; let me know if it doesn't work for you:

.AddMetadata("PageName", t => t.Name)

Jul 2, 2012 at 9:43 PM

Hi Nick,

Thanks for the quick reply. I had indeed missed the overload on AddMetadata that added the type. I was actually looking for something like,

.Export(builder, type => builder.AddMetadata("PageName", type.Etc()))

Rather than,

.Export(builder => builder.AddMetadata("PageName", type => type.Etc()))

The second solution works great.

Thanks,

    Andy

PS. I have one more issue but I'll add this as a separate thread so others can find it easier.