I am trying to get the value of a string property in a unit test. The problem is that the property has no getter. The property is also declared as an AutoProperty and has no backing field.
I am trying to use PropertyInfo.GetValue(...) in the System.Reflection namespace. However I get System.ArgumentException : Property Get method was not found.
I do not own the code of that type so I am unable to add a getter to the property.
How does one get the value of such a property?
You would somehow need to find the backing field that was auto-generated by compiler using Reflection. And then, using its
FieldInfo
you will be able to read its value. And I am not sure if it is possible at all.OK, I have the solution:
With following class,
and following extension method,
You can achieve what you want by using following code: