0

Is it possible to get the field(s) which is used for a property's get and set through Reflection?

For example, if I have this:

private int numb;

public int Number
{
 get => numb;
 set => numb = value;
}

Is it possible to know which backing field Number is using for its set property?

In this case, returning numb.

Moelbeck
  • 591
  • 2
  • 7
  • 29
  • see https://stackoverflow.com/questions/95910/find-a-private-field-with-reflection – Johan Maes May 07 '20 at 06:59
  • 1
    sure - but why would you want to? also in this case the property is completely public so you can access the backing field through the property. – sommmen May 07 '20 at 07:03
  • You can not get the backing field of a property. You can get the fields and the properties by reflection, but the getter/setter are methods and can contain anything also writing/reading to many fields. If, you had to analyze that methods – Sir Rufo May 07 '20 at 07:06
  • The code I'm working on has the functionality to extract its data to a .xml files. These .xml files will be filled with data from the properties, along with the class name, property name, etc. These .xml files can be used for importing it into another instance of the system. Importing is using the private fields for the class, as we are sure that there are no extra code running while setting the value, which may happen for a set property. So we want a test to ensure that fields and properties are named the same (with lower case for properties). – Moelbeck May 07 '20 at 07:42

0 Answers0