Tech Off Post

Single Post Permalink

View Thread: Get a name of derived class in c#
  • User profile image
    PopeDai

    , alekseyfili​ppov wrote

    Is it possible for each subclass of a parent class A to return its own class name as a property?

    There is a constraint in Couchbase .NET client for models to expose a class-name property. As it is reflected in their documentation it can be resolved by using this code:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    class abstract BaseModel {
        public abstract String Type {get;}
    }
     
    class Robot : BaseModel {
        public override String Type {
            get { return "Robot"; }
        }
    }

    Is it possible to automate this from within the BaseModel class?

    abstract class BaseModel {        public virtual String Type {        get { return this.GetType().Name; }    }    }