Define a method in base class that returns the name of itself (using reflection) - subclasses inheri

Posted by Khnle on Stack Overflow See other posts from Stack Overflow or by Khnle
Published on 2010-06-17T06:17:40Z Indexed on 2010/06/17 6:23 UTC
Read the original article Hit count: 176

Filed under:
|

In C#, using reflection, is it possible to define method in the base class that returns its own name (in the form of a string) and have subclasses inherit this behavior in a polymorphic way?

For example:

public class Base
{
    public string getClassName()
    {
        //using reflection, but I don't want to have to type the word "Base" here.
        //in other words, DO NOT WANT  get { return typeof(Base).FullName; }
        return className; //which is the string "Base"
    }
}

public class Subclass : Base
{
    //inherits getClassName(), do not want to override
}

Subclass subclass = new Subclass();
string className = subclass.getClassName(); //className should be assigned "Subclass"  

© Stack Overflow or respective owner

Related posts about c#

Related posts about reflection