like in java I have:
Class.getSuperClass().getDeclaredFields()
how I can know and set private field from a superclass?
I know this is strongly not recommended, but I am testing my application and I need simulate a wrong situation where the id is correct and the name not. But this Id is private.
Like JaredPar suggests, I did the follow:
Thanks to all.
Yes, it is possible to use reflection to set the value of a readonly field after the constructor has run
EDIT
Updated to look in the direct parent type. This solution will likely have issues if the types are generic.
Yes, you can.
For fields, use the
FieldInfo
class. TheBindingFlags.NonPublic
parameter allows you to see private fields.and a small test to prove it works:
This class will let you do it:
http://csharptest.net/browse/src/Library/Reflection/PropertyType.cs
Usage:
BTW, It will work on public/non-public fields or properties. For ease of use you can use the derived class PropertyValue like this: