I'm looking for way to get the type of inherited class from a static method of this class which is defined in the base class. I hope the code snippet makes it more clear.
Here is what I have tried, but this reflects the base type but not the child type:
abstract class Parent()
{
static Parent MyMethod()
{
// I get the parent type but I want the child type
Type thisObjType = MehtodBase.GetCurrentMethod().DeclaringType;
// Fails because I try to instantiate an abstract object
Parent myChildObj = (Parent)Activator.CreateInstance(thisObjType);
// ...
return myChildObj;
}
}
class Child : Parent
{
}
The call to this method failed because the delivered one is the parent type and not the child.
Child myChild = (Child)Child.MyMethod()
I've found this, but it's not the solution I'm looking for: Get derived class type from a base's class static method If possible, I would try to avoid the use of generic.
How do I get the type of the object from the static method? Thank you in advance!