Is it possible? With reflection or any other way?
-
9Élodie: It's possible, but **DON'T DO IT**!!! – Gabe Sep 14 '10 at 05:56
-
Are you trying to change the return value of a property with getter only, or the value of a backing field which is readonly (like `private readonly int primitiveValue = 1;`)? – Elisha Sep 14 '10 at 06:09
-
2@Gabe: In general, I agree. I had to actually use this to debug some code that I did not have the source for. It's useful when you need to force code to do something to expose error behavior when debugging. – Mike Bailey Jul 14 '12 at 16:51
-
1I am required to do so in the matter of deserializing an object where one of the readonly properties is a collection. That's another "good" reason you might want to do so. FYI, it is much easier and cleaner to invoke reflection on the non-readonly backing field. – Ama Dec 10 '19 at 13:28
5 Answers
As other stated, if you need to do that, you're facing a design issue to begin with. Now, if you want to know if it's possible just for the sake of knowing, or if there's no other way on earth to do it, it's indeed possible, with the help of a very small helper library and an extension method.
Consider the following code:
class Person {
int age;
string name;
public int Age { get { return age; } }
public string Name { get { return name; } }
}
// ...
using Mono.Reflection;
using System.Reflection;
// ...
Person person = new Person (27, "jb");
PropertyInfo nameProperty = typeof (Person).GetProperty ("Name");
FieldInfo nameField = nameProperty.GetBackingField ();
nameField.SetValue (person, "jbe");
Using this code, you can get the backing field of a property with just the property, and assign a new value to the backing field. You can read more details about the implementation.
Also note that it works only for simple properties, such as:
public int Age { get { return age; } }
public string Name {
get { return name; }
set { name = value; }
}
public double Velocity { get; private set; }
If you have complex properties with custom code (which includes expression-bodied member like int Answer=> 42;
), the backing field resolver will fail as there is no backing field in such case.

- 98,904
- 14
- 127
- 179

- 17,319
- 2
- 67
- 67
An alternate to Simon Mattes answer would be
Assuming you have:
public class MyClass
{
public int MyNumber {get;}
}
You could do this if its for test purpose:
var field = typeof(MyClass).GetField("<MyNumber>k__BackingField", BindingFlags.Instance | BindingFlags.NonPublic);
field.SetValue(anIstanceOfMyClass, 3);

- 852
- 8
- 9
As a very dirty workaround for auto generated read only properties:
class MyTestClass
{
public string MyProperty { get; }
public MyTestClass( string MyProperty )
{
this.MyProperty = MyProperty;
}
}
You can modify the auto generated backing field in the following way:
MyTestClass MyClass = new MyTestClass( "Hello" );
FieldInfo MyWriteableField = MyClass.GetType().GetRuntimeFields().Where( a => Regex.IsMatch( a.Name, $"\\A<{nameof( MyClass.MyProperty )}>k__BackingField\\Z" ) ).FirstOrDefault();
MyWriteableField.SetValue( MyClass, "Another new value" );
PS: when you are using a .NET version < 4.6 you might have to change some of the code to work. Enjoy!

- 4,866
- 2
- 33
- 53
it depends on the property. if it's a computed property - no, not unless you know what it's based on. if it's just an accessor to a private field, then you can try to modify the field.
in general, however, it's a very bad idea, as you likely have no knowledge of what side-effects this will cause.

- 3,029
- 3
- 29
- 48
PropertyInfo isReadOnly = typeof(System.Collections.Specialized.NameValueCollection).GetProperty("IsReadOnly", BindingFlags.Instance| BindingFlags.NonPublic);
//Remove the readonly property
isReadOnly.SetValue(param, false, null);
//.............put your code here.....
// Set readonly property
isReadOnly.SetValue(param, true, null);
-
I've tried this and it doesn't work. All you are doing is setting the property to false then back to true. – Pellet Nov 30 '18 at 05:07