views:

62

answers:

4

Is there a way to reference the class (i.e. Type) that inherits a abstract class?

class abstract Monster
{
    string Weakness { get; }
    string Vice { get; }

    Type WhatIAm
    {
        get { /* somehow return the Vampire type here? */ }
    }
}

class Vampire : Monster
{
    string Weakness { get { return "sunlight"; }
    string Vice { get { return "drinks blood"; } }
}

//somewhere else in code...
Vampire dracula = new Vampire();
Type t = dracula.WhatIAm; // t = Vampire

For those who were curious... what I'm doing: I want to know when my website was last published. .GetExecutingAssembly worked perfectly until I took the dll out of my solution. After that, the BuildDate was always the last build date of the utility dll, not the website's dll.

namespace Web.BaseObjects
{
    public abstract class Global : HttpApplication
    {
        /// <summary>
        /// Gets the last build date of the website
        /// </summary>
        /// <remarks>This is the last write time of the website</remarks>
        /// <returns></returns>
        public DateTime BuildDate
        {
            get
            {
                // OLD (was also static)
                //return File.GetLastWriteTime(
                //    System.Reflection.Assembly.GetExecutingAssembly.Location);
                return File.GetLastWriteTime(
                    System.Reflection.Assembly.GetAssembly(this.GetType()).Location);
            }
        }
    }
}
+5  A: 

Use the GetType() method. It's virtual so it will behave polymorphically.

Type WhatAmI {
  get { return this.GetType(); }
}
JaredPar
Or, better yet, just use GetType().
Etienne de Martel
Also note that GetType() will get the actual type of whatever you're using, even if you've cast it into something else (for reference types, of course). So if you have an interface like IMonster, you can use IMonster.GetType() to see what it actually is, likewise for your Monster abstract base.
CodexArcanum
A: 

You don't need the Monster.WhatIAm property. C# has the "is" operator.

Matthew Graybosch
But I want to `return` the value, not simply compare it.
Brad
Why do you need to return it?
Matthew Graybosch
@Matthew, I added more about what I am doing
Brad
+1  A: 

Looks like you're just looking to get the type, which both of the above answers provide well. From the way you asked the question, I hope Monster doesn't have any code that depends on Vampire. That sounds like it would be an example of a violation of the Dependency Inversion Principle, and lead to more brittle code.

Visionary Software Solutions
This was my first thought, a base class should never need to know the type of the inheriting class. Hopefully it's not running a switch on all the known subclasses. If that is what is happening, DIP is your friend. However it does seems that he just wants a method in the base class that outputs the current type, mostly for debugging purposes. Sounds ok to me because it's not depending on behavior from the subclasses. DIP would suggest that all subclasses should define a getType method. However a base class already defines it, so passes the DIP test.
Juan Mendes
A: 

You can also get the base class information directly from the inherited class (Vampire) using the following snippet:

 Type type = this.GetType();     
 Console.WriteLine("\tBase class = " + type.BaseType.FullName);
Ravi Gummadi