Convert System.Type to derived script in base class

I have an abstract base class and a class that derives from it, see below.
In the base.Awake() I need to examine the derived class and get information on methods with a custom attribute set (and other stuff).


In the base.Awake(), “this.GetType” is a System.Type and the ToString() outputs it as, “ADerivedClass”.

How can I cast the System.Type so I can access it as a true ADerivedClass?

Thanks.
– Paul


public abstract class MyBaseClass<T> : MonoBehaviour {
     private void Awake() {
            Debug.Log("CLASS: " + this.GetType().ToString());
     }
}

public class ADerivedClass : MyBaseClass<ADerivedClass > {   
     // Other code goes here.
}

This seems like the wrong solution for whatever problem you are trying to solve.

If you need to access members from a child class, then it would make more sense to have them as virtual/abstract members of the parent class and simply override them. On the other hand, if these members need to only be a part of the child class then you can move whatever code relies on those members into a virtual/abstract function and override the functionality there.

If you really need to cast the child class in the parent, then you can do so directly by just checking if the type is correct at runtime:

if (this is ADerivedClass)
{
    ADerivedClass _Child = (ADerivedClass)this;
    // Do whatever processing you need with _Child
}

There is no way to dynamically cast an object to a static type at runtime from a System.Type, however there are ways to dynamically manipulate objects using data from System.Type and the System.Reflection namespace - this should really be avoided if you can though, as it is not cheap.