1

Possible Duplicate:
Can I change a private readonly field in C# using reflection?

I need to modify a data structure class, but the data is hidden behind a read-only property. Can I, using any technique, modify private variables within a class?

What if I was able to peer into the object and note down exact byte positions of the variables I need. Can I then use unsafe code to directly modify memory at those offsets? Is anything like this possible?

Community
  • 1
  • 1
Robin Rodricks
  • 110,798
  • 141
  • 398
  • 607
  • 4
    see here: http://stackoverflow.com/questions/934930/can-i-change-a-private-readonly-field-in-c-sharp-using-reflection – Sebastian Piu Dec 13 '11 at 10:27

1 Answers1

2

I think you could use Reflection tecniques to modify the variables of the instance. It is not dificult:

typeof(MyClassType).GetField("TheField",BindingFlags.Instance|BindingFlags.NonPublic).SetValue(ClassInstance,NewValue);

Remember to include the system.reflection namespace.

Jonathan
  • 11,809
  • 5
  • 57
  • 91