I need to modify a property value which has only "get" option. I have found some trick with using GetField but I did not succeed.
See below the code:
namespace ConsoleApplication1
{
abstract class Test
{
private int testValue;
protected int TestValue {
get { return testValue; }
}
}
class Myclass : Test
{
public Myclass(): base(){}
}
class Program
{
static void Main(string[] args)
{
Myclass test = new Myclass();
var field = typeof(Myclass).GetField("<TestValue>k__BackingField", BindingFlags.NonPublic | BindingFlags.Instance);
field.SetValue(test, 3);
}
}
}
Actually, I would like to set "TestValue" with a the value 3 but it is not working. I do probably something wrong.