How to get value of string via Reflection?

I have variable “img : String” and I try ot get the value, but get empty string. Example:

class Test {
    static function main() {
        
        new Test2().me();
        
    }
}

class Test2 {
    public function new()
    {
       
    }
    var img : String = "this is haxe";
    public function test() {
        trace(Reflect.field(this,"img"));

    }
}

Do you mean Test2 ().test () or is me another function?

Yes, Test2().test is the correct function. My mistake

try.haxe.org has dead-code elimination enabled by default. img was not referenced anywhere, so it removed it. Adding @:keep to a class or field that is only referenced by reflection can help

http://try.haxe.org/#B0c99

1 Like

Thank you !!! It really works