mardi 13 mai 2014

MEF load types that implement interface and match attributes


Vote count:

0




Lets say i have an interface IFileLoader and the attribute FileTypeAttribute


And I implement IFileLoader in JPEGLoader with [FileType(".jpg")]


Can i use MEF to load the class (JPEGLoader) that implements IFileLoader and matches .jpg as file extension so I can implement the following method:



public void IFileLoader GetLoader(string filename);


Can this be done with MEF, or should I stick to this:



var allTypes = assemblies.SelectMany(a => a.GetTypes());
var classes = allTypes.Where(t => t.IsClass && ! t.IsAbstract);
var fileLoaders = classes.where(t => typeof(IFileLoader).IsAssignableFrom(t));
var forType = fileLoaders.Where(t => t.GetAtributeValue<FileTypeAttribute,string>(t => t.FileType, string.Empty) == fileType);
var loaderInstances = fileLoaders.Select(t => Activator.CreateInstance(t) as IFileLoader);


Or the above turned into ILookup at least, or maybe something else that I haven't considered?


I'd like to be able to implement the IFileLoader in different assemblies of the project or even in plugin assemblies.




leppie

66.1k

asked 2 mins ago






Aucun commentaire:

Enregistrer un commentaire