views:

39

answers:

1

Is it possible get the assembly information from an imported MEF function? I need to know the assembly version and name of the Plugin control that contains the function. Tried the following, but it just returns the System.ComponentModel.Composition version.

foreach (Lazy<Func<int>, IMetadata> func in PluginFuncs)
{
    // get assembly information of the Plugin control for the imported function 
    string version = func.GetType().Assembly.GetName().Version.ToString();
    Console.WriteLine(version);
}

Another alternative would be to use hardcoded values in the metadata, but I thought this would not be maintainable. It would be easy to forget to change those values when the version changed.

+1  A: 

You need to check the type from within func.Value, not the Lazy<T,TMeta> wrapping it. Try:

Func<int> lambdaFunc = func.Value;
Delegate del = lambdaFunc;
string version = del.Method.ReflectedType.Assembly.GetName().Version.ToString();

However, realize that this will evaluate the Lazy<T> at this point - but this is required, because the object where you are trying to get the type hasn't be constructed until you evaluate that.

Reed Copsey
Thanks for the suggestion, but now it's returning the version for mscorlib. Any more ideas?
John_Sheares
@John_Sheares: Oh, damn - that makes sense - Func<T> is System.Func<T>... mmm... let me think
Reed Copsey
@John_Sheares: I believe the above will now work. By assigning to a delegate, you can get the type on which the method is defined via the MethodInfo returned in Delegate.Method.
Reed Copsey
Reed, it worked! Thanks again for your incredibly fast help!
John_Sheares