Search code examples
dphobos

Get the variable values at runtime using reflection in Dlang


Is it possible to get the class/struct/other variables value during runtime in dlang to get/set its value? If yes how to do that please provide example. And also is it possible to get the runtime variable value?

Ex:

class S{ int svariable = 5;}
class B { int bvariable = 10;}
void printValue(T, T instanceVariable, string variableName) {
    writeln("Value of ",  variableName, "=", instanceVariable.variableName);
}

Output:

Value of svariable = 5;
Value of bvariable = 10;


Solution

  • There is a library named witchcraft that allows for runtime reflection. There are examples of how to use it on that page.