我試圖通過反射設置一個值。我創造了這個小測試程序通過反射設置值不起作用
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();
}
Steping thorugh程序我看到了不正確的命令s.SetValue(headers, 99, null);
但是在運行時的setValue的headers.AccountNum值保持爲1。
我錯過了一個明顯的步驟?
可變結構是邪惡的。 http://blogs.msdn.com/ericlippert/archive/2008/05/14/mutating-readonly-structs.aspx –