2

I've got a base class BaseClass and a bunch of derived classes MyClass1, MyClass2. Now, I want to be able to get Type of derived class at the level of BaseClass. I found online some workaround for how to do this:

public with sharing abstract class BaseClass {
    public Type getType() { // returns MyClass1.class or MyClass2.class
        return Type.forName(String.valueOf(this).substringBefore(':'));
    }
}

Alternatively, I can declare getType() as abstract or virtual and then override it in derived classes and explicitly make it to return MyClass1.type or MyClass2.type. But both those solutions are clunky. Is there a better way to do this? Something like this.class would be perfect, but it doesn't exist.

2

At this time, Apex doesn't have much in the way of introspection/reflection.

That getType() method that you found looks like the best solution we currently have available to us.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.