Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GetExecutingAssembly() for derived class in different assembly

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?

like image 674
djs Avatar asked Apr 08 '14 15:04

djs


People also ask

What is assembly GetExecutingAssembly ()?

Assembly property to get the currently executing assembly based on a type contained in that assembly. It also calls the GetExecutingAssembly method to show that it returns an Assembly object that represents the same assembly.

What is System reflection assembly?

Namespace: System.Reflection. Summary. Defines an Assembly, which is a reusable, versionable, and self-describing building block of a common language runtime application. C# Syntax: [Serializable]


1 Answers

Use this.GetType().Assembly instead.

like image 96
Daniel A. White Avatar answered Sep 19 '22 18:09

Daniel A. White