function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion

Dynamically get Class Variable Value

Is there a equivalent  of sObject.get() for sObject's but for Classes? 


Need a way to dynamically retrieve the value of a class variable of an instantiated object as you don't know which variable you want until runtime. (REMEMBER: THIS IS NOT AN SOBJECT)


--- Psuedo Example ----

Class test {

     public name;

     public email;



Test t = new Test();



Found this in the " Apex  Code Developer's Guide"   :-


However, with class variables Apex also uses dot notation to reference member variables. Those member variables might refer to other class instances, or they might refer to an sObject which has its own dot notation rules to refer to field names (possibly navigating foreign keys).
Once you enter an sObject field in the expression, the remainder of the expression stays within the sObject domain, that is,sObject fields cannot refer back to Apex expressions.


For instance, if you have the following class:
public class c {
c1 c1 = new c1();
class c1 { c2 c2; }
class c2 { Account a; }
Then the following expressions are all legal:


 Conversely:- - is legal


Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.



I found a solution to this question. It involves serializing your apex class.

Map<String, Object> m = (Map<String, Object>)JSON.deserializeUntyped(JSON.serialize( -- Class Var Here -- ));