views:

33

answers:

2

In one C# project have a base class like this:

public abstract class AbstractVersionedBase
{
    public string Version { get { 
        return Assembly.GetExecutingAssembly().GetName().Version.ToString(); 
    }}
}

This project has a version of 0.0.1.1.

In another C# project I have a class that inherits from AbstractVersionedBase like this:

public class Versioned : AbstractVersionedBase
{
}

This project has a version of 0.0.1.2.

When I call the property on the concrete class, it returns the version of the assembly that the abstract class is defined in.

var versioned = new Versioned();
Console.WriteLine("Version" + versioned.Version);  
//writes 0.0.1.1, but I want 0.0.1.2

I would like the descendant class to return the version of its assembly, but not have to rewrite the code to do that in the descendant class. Do you have any ideas?

+1  A: 

Use this.GetType().Assembly instead.

Eric Mickelsen
+1  A: 

You need to get the assembly where the dynamic type is declared with GetType().Assembly:

public string Version
{
    get
    {
        return GetType().Assembly.GetName().Version.ToString(); 
    }
}
CodeInChaos