Question

I have a plug-in architecture where an abstract base class is defined in the main application. It uses reflection to load assemblies that have derived implementations of the base class. I would like to get the version information of the assembly that contains the derived object. Here is my base class:

namespace My.AppNameSpace
{
    public abstract class BaseClass
    {
        public Version Version
        {
            get
            {
                Assembly assembly = Assembly.GetExecutingAssembly();
                FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(assembly.Location);
                string version = fvi.FileVersion;
                return new Version(version);
            }
        }

        public abstract void DoStuff();
    }
}

Since the base class is part of the main application assembly, this always returns the version information of the executing application, not the .dll that contains the implementation of the derived class. I can change the Version accessor to be abstract, and it works as I would like. But then I need to add those same several lines of code to every implementing plug-in.

Is there some trick to getting the version information of the derived object from code in a base class that exists in a separate assembly?

Was it helpful?

Solution

Use this.GetType().Assembly instead.

OTHER TIPS

If, from your abstract class, you call this.GetType(), you will get the runtime type of the actual class. From there, you could get the Assembly property of that type.

    public Version Version
    {
        get
        {
            Assembly assembly = this.GetType().Assembly;
            FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(assembly.Location);
            string version = fvi.FileVersion;
            return new Version(version);
        }
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top