How to get the assembly information of an imported UserControl through MEF, without instantiating UserControl?
Is it possible get the assembly information from an imported MEF Type, without instantiating开发者_JS百科 a type? I need to know the assembly name and version of the Plugin control that contains the Type. Tried the following, but it just returns the System.ComponentModel.Composition.
foreach (Lazy<UserControl, IMetadata> content in contents)
{
// get assembly information of the Plugin control for the imported function
string definingAssembly = content.GetType().Assembly.GetName();
Console.WriteLine(definingAssembly);
}
To avoid maintenance issues if possible I want to avoid specifying this information in Metadata.
This is a FAQ that pops up on the MEF forum every now and then. (I'm sure that there are several better threads that include an answer, but I'm unable to find one immediately).
MEF does not support inspecting the part type of a lazy import (without instantiating that part) out of the box. This is probably because of two elements in its design philosophy:
MEF encourages loose coupling. You should specify and use imports based on their contract. Relying on the actual class type of an import would prevent you from changing the composition in the future.
MEF allows for lazy loading of types. Suppose there would be a
Lazy.PartType
property which you can use to inspect he actual type of the part. This would force the part type and the containing assembly to be loaded. Then you decide based on thePartType
that you don't want to instantiate that particular part, and the assembly load was for nothing.(As far as I can tell, currently MEF does not yet exploit the second design feature out of the box. AssemblyCatalog will inspect all types immediately when
AssemblyCatalog.Parts
is called anyway. But there is a sample in the MEF sources that shows how to cache information about assemblies to delay or avoid assembly loads.)
So instead of relying on the exact type of the part, you should add some metadata that you can use to do the selection of an import.
What you tried should fail, as the Assembly never can be the dynamically loaded module assembly.
I guess if you don't instantiating a type, that module assembly is not even loaded into memory.
Did you try to post to MEF discussion board yet? There you may meet the developers.
http://mef.codeplex.com/discussions
I've been thinking about this problem as I have run into this issue myself. Where I want to create the instances on demand rather than have them created for me by the composition.
The scenario I have is that I don't need to know the particular type but I may need to create multiple instances of the type, use them, and dispose them when appropriate. The solution I came up with is instead of using MEF to load the concrete types and be forced into having it create instances for me, use MEF to load factories that know how to create instances of the type I want to control the lifetime.
MEF would load and create the factory and then I can use the factory to create instances that I can use and dispose of at will.
精彩评论