Skip to main content
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link
URL Rewriter Bot
URL Rewriter Bot

Yet another gotcha: readonly values can be changed by "devious" code via reflection.

var fi = this.GetType()
             .BaseType
             .GetField("_someField", 
                       BindingFlags.Instance | BindingFlags.NonPublic);
fi.SetValue(this, 1);

[Can I change a private readonly inherited field in C# using reflection?][1] [1]: httphttps://stackoverflow.com/questions/1401458/can-i-change-a-private-readonly-inherited-field-in-c-using-reflection/1401499#1401499

Yet another gotcha: readonly values can be changed by "devious" code via reflection.

var fi = this.GetType()
             .BaseType
             .GetField("_someField", 
                       BindingFlags.Instance | BindingFlags.NonPublic);
fi.SetValue(this, 1);

[Can I change a private readonly inherited field in C# using reflection?][1] [1]: http://stackoverflow.com/questions/1401458/can-i-change-a-private-readonly-inherited-field-in-c-using-reflection/1401499#1401499

Yet another gotcha: readonly values can be changed by "devious" code via reflection.

var fi = this.GetType()
             .BaseType
             .GetField("_someField", 
                       BindingFlags.Instance | BindingFlags.NonPublic);
fi.SetValue(this, 1);

[Can I change a private readonly inherited field in C# using reflection?][1] [1]: https://stackoverflow.com/questions/1401458/can-i-change-a-private-readonly-inherited-field-in-c-using-reflection/1401499#1401499

added 67 characters in body
Source Link
Kjartan
  • 18.9k
  • 16
  • 73
  • 98

Yet another gotcha: readonly values can be changed by "devious" code via reflection.

var fi = this.GetType()
             .BaseType
             .GetField("_someField", 
                       BindingFlags.Instance | BindingFlags.NonPublic);
fi.SetValue(this, 1);

[Can I change a private readonly inherited field in C# using reflection?][1] [1]: http://stackoverflow.com/questions/1401458/can-i-change-a-private-readonly-inherited-field-in-c-using-reflection/1401499#1401499

Yet another gotcha: readonly values can be changed by "devious" code via reflection.

var fi = this.GetType().BaseType.GetField("_someField", BindingFlags.Instance | BindingFlags.NonPublic);
fi.SetValue(this, 1);

[Can I change a private readonly inherited field in C# using reflection?][1] [1]: http://stackoverflow.com/questions/1401458/can-i-change-a-private-readonly-inherited-field-in-c-using-reflection/1401499#1401499

Yet another gotcha: readonly values can be changed by "devious" code via reflection.

var fi = this.GetType()
             .BaseType
             .GetField("_someField", 
                       BindingFlags.Instance | BindingFlags.NonPublic);
fi.SetValue(this, 1);

[Can I change a private readonly inherited field in C# using reflection?][1] [1]: http://stackoverflow.com/questions/1401458/can-i-change-a-private-readonly-inherited-field-in-c-using-reflection/1401499#1401499

Source Link
Greg
  • 23.4k
  • 11
  • 58
  • 79

Yet another gotcha: readonly values can be changed by "devious" code via reflection.

var fi = this.GetType().BaseType.GetField("_someField", BindingFlags.Instance | BindingFlags.NonPublic);
fi.SetValue(this, 1);

[Can I change a private readonly inherited field in C# using reflection?][1] [1]: http://stackoverflow.com/questions/1401458/can-i-change-a-private-readonly-inherited-field-in-c-using-reflection/1401499#1401499