If I declare the following in my class:
private int? MyID = null;
And then attempt to access it via reflection, it won’t be able to find it. What I mean by that is, the below will set gProp to null:
gType = refObj.GetType();
gProp = gType.GetProperty(PropertyName, System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
However, it will work fine if I instead declare it as:
private int? MyID { get; set; }
This isn’t at all surprising to me as I already knew this to be the case. However, I wanted to confirm; is there anyway to make the first declaration work with reflection, or do I have provide a Getter/Setter in order for reflection to work?
Thanks!
You need the
GetFieldmethod (instead ofGetProperty) for Fields.Sample
More Information