c# - Issue with system.reflection, GetFields not returning everything -


im having bit of issue system.reflection. please see attached code:

class program {     public static fieldinfo[] reflectionmethod(object obj)     {         var flags = bindingflags.instance | bindingflags.static | bindingflags.public | bindingflags.nonpublic | bindingflags.declaredonly;         return obj.gettype().getfields(flags);     }         static void main()     {         var test = new test() { id = 0, age = 12, height = 24, issomething = true, name = "greg", weight = 100 };         var res = reflectionmethod(test);     } }      public class test {     public int id { get; set; }     public string name { get; set; }     public int age { get; set; }     public bool issomething { get; set; }     public int weight { get; set; }     public int height { get; set; }     public int calculationresult => weight * height;      public test()     {      } } 

it seems though getfields method not getting calculated property calculationresult. i'm assuming there flag need use, can't figure out 1 is.

thanks in advance , i'll happily provide more info if necessary.

that because property , not field.

=> syntactic sugar getter property. equivelant to:

public int calculationresult  {         {        return weight * height;     } } 

so need use .getproperties(flags)


Comments