public static IEnumerable <KeyValuePair <Type, M> > GetExportTypesWithMetadata <T, M>( this CompositionContainer mefcontainer) where T : class where M : class { // need to examine each type to see if they have the correct export attribute and metadata foreach (var type in mefcontainer.GetExportTypes <T>()) { // should just be one if more than one will throw exception // metadata or export attribute has to implement the interface var metadataAttribute = type.GetCustomAttributes() .SelectMany( a => a.GetType() .GetCustomAttributes() .OfType <MetadataAttributeAttribute>() .Concat <Attribute>(new[] { a }.OfType <ExportAttribute>())) .OfType <M>().SingleOrDefault(); // if we found the correct metadata if (metadataAttribute != null) { // return the lazy factory yield return(new KeyValuePair <Type, M>(type, metadataAttribute)); } } }
/// <summary> /// Gets all types within the <see cref="CompositionContainer" />'s <see cref="CompositionContainer.Catalog" /> /// which implement the specified <paramref name="type" />. /// </summary> /// <param name="container">The container.</param> /// <param name="type">The type.</param> /// <returns>Enumeration of derived / implementing <see cref="Type" />s.</returns> public static IEnumerable <Type> GetExportTypesThatImplement(this CompositionContainer container, Type type) { return(container.GetExportTypes() .Where(typePart => typePart.IsAssignableToType(type)).ToList()); }