Can MEF be used to get System.Type classes marked with [Export]?

I have successfully used MEF to get exported instances of the class. However, I came across a situation where I need to list a set of exported derived classes without creating them. I looked at the CompositionContainer documentation and it seems to me that it can return instances of objects.

I know that I can have a static type field in each derived class and export it or make my own reflection, but I would like to know if there is a built-in way to mark classes with the [Export] attribute and then list them System.Type .

+7
mef
source share
7 answers

As leppie said, there is no built-in way to do this. This is by design. One-to-one mapping between exports and types is not necessary (for example, any number of parts may have export of properties of type String). In addition, with various programming models, a part can be obtained from a configuration file or a dynamic programming language, so trying to get the associated CLR type may not make much sense.

+4
source share

Depending on what you are trying to do, you can potentially also use System.ComponentModel.Composition.ReflectionModel.ReflectionModelServices, which were introduced by the API to support default directory caching. Assuming you are using a standard attributed programming model, and know that all of your [Export] are at the type level (that is, they are not members), then you can call GetPartType (part) for each part of your directory to get type.

As Daniel noted, if you use other programming models, this will not work for you, but if you use only the default directories that come with MEF, then it should do the job.

+4
source share

Isn't that what you are looking for?

  public static IEnumerable<Type> GetExportedTypes<T>() { return catalog.Parts .Where(part => IsPartOfType(part, typeof(T).FullName)) .Select(part => ReflectionModelServices.GetPartType(part).Value); } private static bool IsPartOfType(ComposablePartDefinition part, string exportTypeIdentity) { return (part.ExportDefinitions.Any( def => def.Metadata.ContainsKey("ExportTypeIdentity") && def.Metadata["ExportTypeIdentity"].Equals(exportTypeIdentity))); } 
+4
source share

Is there something wrong with using Reflection for this?

If not, then my answer is :)

Edit:

There is no built-in way to get all types in an assembly with a specific attribute.

+1
source share

Usually you do not need to select export by type. Instead, you can find the โ€œcorrectโ€ metadata based export.

See the section of the MEF Programming Guide for Export and Metadata .

+1
source share

It may be a little old, but I think it's good to post the correct answer here, because the question comes first in a Google search:

Yes, you can do it thanks to Ricci Gian Maria.

http://www.codewrecks.com/blog/index.php/2012/05/08/getting-the-list-of-type-associated-to-a-given-export-in-mef/

+1
source share

You can consider the Export attribute as a custom attribute. I did not test performance when the assembly is very large.

  Type[] GetType(Assembly assembly) { return assembly.GetTypes().Where(type => Attribute.GetCustomAttribute(type, typeof(ExportAttribute)).Length > 0).ToArray(); } 
+1
source share

All Articles