I am trying to set a value through reflection. I created this little test program
struct headerIndexes
{
public int AccountNum{ get; set; }
public int other { get; set; }
public int items { get; set; }
}
static void Main(string[] args)
{
headerIndexes headers = new headerIndexes();
headers.AccountNum = 1;
Console.WriteLine("Old val: {0}", headers.AccountNum);
foreach (var s in headers.GetType().GetProperties())
{
if (s.Name == "AccountNum")
s.SetValue(headers, 99, null);
}
Console.WriteLine("New val: {0}", headers.AccountNum);
Console.ReadKey();
}
When I run the program, I see it correctly, the command s.SetValue(headers, 99, null);, however the value of headers.AccountNum remains equal to 1 when running setValue.
Did I miss the obvious step?
source
share