2

I have an application using MEF to load extensions, and I'd like to be able to retrieve the (assembly) version information before MEF actually loads the extension. I believe this could be done if the assembly information was embedded in the extension's metadata. Unfortunately, it seems the metadata only accepts design time constant values. Maybe someone can tell me otherwise?

Ideally, I'd like to be able to declare the metadata similar to this:

[Export(typeof(IExtension))]
[ExportMetadata("Description", "Spell Checker")]
[ExportMetadata("AssemblyVersionInformation", "???????")]
public class MyExtension : IExtension
{
   ...
}

Where my metadata interface is defined as:

public interface IExtensionMetadata
{
   string Description { get; }
   string AssemblyVersionInformation { get; }
}

I've got no idea on what do do about the "???????" in the first part.

I acknowledge my question is very similar to: How do I get the version number of each DLL that has my MEF plugins? But I want to be able to access this information before the plugin is loaded.

Community
  • 1
  • 1
Eric
  • 2,207
  • 2
  • 16
  • 16

2 Answers2

0

So you want to get it before the part is instantiated, right? You could get the ExportDefinition for the part, and then use ReflectionModel.ReflectionModelServices.GetPartType to get the type. From there you can get the assembly, and read its version information directly.

Here's an example in VB:

 Dim objPartDef As Primitives.ComposablePartDefinition

 objPartDef = myCatalog.Parts.Where(
       Function(objPart) objPart.ExportDefinitions.Count > 0 AndAlso
           objPart.ExportDefinitions(0).Metadata.ContainsKey("Description") AndAlso
        CStr(objPart.ExportDefinitions(0).Metadata("Description")) = "Spell Checker"
            ).FirstOrDefault()

 Dim objVersion as System.Version = ReflectionModel.ReflectionModelServices.GetPartType(objPartDef).Value.Assembly.GetName.Version

Since the question is tagged C#, here's an attempt at translation. I don't have a C# compiler and don't use the language, so there's probably syntax errors (I'll almost certainly get the lambda wrong):

Primitives.ComposablePartDefinition objPartDef;

objPartDef = myCatalog.Parts.Where( objPart => objPart.ExportDefinitions.Count > 0 &&
   objPart.ExportDefinitions(0).Metadata.ContainsKey("Description") && 
   Convert.ToString(objPart.ExportDefinitions(0).Metadata("Description")) == "Spell Checker").FirstOrDefault();

System.Version objVersion = ReflectionModel.ReflectionModelServices.GetPartType(objPartDef).Value.Assembly.GetName.Version
Matt
  • 548
  • 9
  • 24
0

cat core/maven-metadata.xml | tail -5 | head -1 | grep -o '[0-9].*' |awk -F '<' '{print $1}'