Get the class name from the class that extends it
I have a class like this
public class BaseClass
{
public string Request { get; set;}
}
and I have a class like this:
public class ExtendClass : BaseClass
{
}
Thus, the Request property will always be named ExtendClass. So it will actually beRequest="ExtendClass"
I have a lot of classes that extend BaseClass. I know I can just pass a string, but is it possible to do this?
source to share
You can use object.GetType
which will always return the type from the top in the hierarchy (so the last class):
public string Request
{
get
{
return this.GetType().Name;
}
}
Name
will return the short name of the type with no namespace. If you want that too, you should use FullName
.
source to share
You have several options. For example, you can use reflections:
public string Request { get {return this.GetType().Name; }}
Or you can make it more explicit, with an abstract property (and this way you can specify more than just the class names):
public abstract class BaseClass
{
public abstract string Request { get; }
}
class ExtendClass : BaseClass
{
public override string Request { get {return "ExtendClass"; } }
}
source to share