9

I've got a number of classes which implement IDessertPlugin. These are found in various DLLs which I use MEF to spin up instances of them to use as plug-in functionality in my application.

So what I'm wanting to do is display the version number of the DLLs from which I've loaded plugins using MEF. One or more plugins are defined in one or more DLLs which I load up in my application.

Right now I do something like so:

var catalog = new AggregateCatalog();
catalog.Catalogs.Add(
   new DirectoryCatalog(Path.Combine(
      Path.GetDirectoryName(Assembly.GetExecutingAssembly().location), "Plugins")));

var container = new CompositionContainer(catalog);

container.ComposeParts(this);

And that will load up plugins just fine from the Plugins subdirectory where my application runs.

Doing something like

catalog.Catalogs.First().Parts.First().GetType().Assembly.FullName

just returns "System.ComponentModel.Composition, Version=4.0.0.0, ..."

What I was hoping to be able to know was that I've got Version 1.0 of CakePlugins.dll and Version 1.1 of IceCreamPlugins.dll. The plugins themselves don't have a version attribute about them - I'm wanting to rely upon the version of the DLL. Hope that makes sense.

I haven't figured out to know which DLLs I'm using there so that I can call Assembly.GetName().Version on them.

Ideas?


Solution:

So, the solution to my problem was pretty straightforward after the parts have been composed.

My plugin managing code has an entry like so:

[ImportMany(typeof(IDessertPlugin)]
private IEnumerable<IDessertPluing> dessertPlugins;

and once the container parts composition has taken place, I could iterate through my plug-ins like so:

foreach(var plugin in dessertPlugins)
{
   Console.WriteLine(Assembly.GetAssembly(plugin.GetType()).GetName().Version.ToString());
}
itsmatt
  • 31,265
  • 10
  • 100
  • 164
  • You should post your solution as an answer and mark it as accepted rather than posting it in your question. – tomfanning Aug 07 '12 at 15:44

2 Answers2

2

You can get assembly info from different properties AssemblyVersion, AssemblyFileVersion and AssemblyDescription.

                /// <summary>
                /// This class provide inforamtion about product version.
                /// </summary>
                public class ProductVersion
                {
                    private readonly FileVersionInfo fileVersionInfo;

                    private readonly AssemblyName assemblyName;


                    private ProductVersion(Type type)
                    {
                        // it done this way to prevent situation 
                        // when site has limited permissions to work with assemblies.
                        var assembly = Assembly.GetAssembly(type);
                        fileVersionInfo = FileVersionInfo.GetVersionInfo(assembly.Location);
                        assemblyName = new AssemblyName(assembly.FullName);
                    }

                    public string AssemblyFileVersion
                    {
                        get
                        {
                            return fileVersionInfo.FileVersion;
                        }
                    }

                    public string AssemblyVersion
                    {
                        get
                        {
                            return assemblyName.Version.ToString();
                        }
                    }



                }
user854301
  • 5,383
  • 3
  • 28
  • 37
  • Hey, thanks for your response. My issue wasn't in figuring out how to find out the info from the assembly but how to get assembly info from the MEF objects themselves, which wasn't the way to do it anyhow. Once I'd composed the parts in the container, my objects where created and then I could just iterate through them and get the info I needed. – itsmatt Aug 07 '12 at 15:50
1

So, the solution to my problem was pretty straightforward after the parts have been composed. I was trying to dig down into the MEF objects themselves rather than look into the container that holds all the plug-ins I've loaded. The answer was to totally ignore the fact of how those plug-ins were being loaded and just look at the instantiated objects themselves.

My plugin managing code has an entry like so:

[ImportMany(typeof(IDessertPlugin)]
private IEnumerable<IDessertPluing> dessertPlugins;

and once the container parts composition has taken place, I could iterate through my plug-ins like so:

foreach(var plugin in dessertPlugins)
{
   Console.WriteLine(Assembly.GetAssembly(plugin.GetType()).GetName().Version.ToString());
}
itsmatt
  • 31,265
  • 10
  • 100
  • 164