简体   繁体   中英

Get the name of class inheriting from base class in C#

I have the following base class

class BaseClass
{
//Want to get the name of class A only, even if B is inherited from A
}

class A : BaseClass
{
}

class B : A
{
}

Can someone help me with getting the name of class A only from the base class.

Thanks

public abstract class BaseClass
{
  public void GetInheritorName()
  {
    var nameIs = this.InheritorTellMeYourName();
  }

  protected abstract string InheritorTellMeYourName();
}

public class A : BaseClass
{
  protected sealed override string InheritorTellMeYourName()
  {
     return typeof(A).Name;
  }
}

public class B : A
{
}

Like someone mentioned in the comments, you shouldn't usually need anything like that...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM