Question

Is it possible to get the type of a class exported as an interface via mef using a contract name.

exemple, I want to get the type MailViewModel:

[Export(typeof(IPlugin), "Mail")
public class MailViewModel: IPlugin { }

with MEF I can get a Lazy for the contract name "Mail" but I don't know how to get the type MailViewModel.

I need to know this type because I "can" have on the class exported as IPlugin a specific attribute. Depending on this attribute I will allow or not to create the value of this plugin (in a navigation scenario).

when I export my class under their original type with [Export] I can write this to know if the specific attribute decorate my class :

Attribute.GetCustomAttributes(typeof(myviewmodel), typeof(myattribute));

Knowing the type of the export and the contractname (IPlugin and "Mail"), how can I know if the exported class is or isn't decorate with a specific attribute (without instantiate it).

Was it helpful?

Solution

this will give you all the types that have an export of a certain type and contract name. This is my take on this blog http://www.codewrecks.com/blog/index.php/2012/05/08/getting-the-list-of-type-associated-to-a-given-export-in-mef/

    private static IEnumerable<Type> GetExportTypes(ComposablePartCatalog catalog, Type type, string contractName)
    {
        return catalog.Parts.Where(
            part =>
            part.ExportDefinitions.Any(
                e =>
                e.ContractName == contractName && e.Metadata.ContainsKey("ExportTypeIdentity") &&
                e.Metadata["ExportTypeIdentity"].Equals(
                    type.FullName))).Select(part => ReflectionModelServices.GetPartType(part).Value);
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top