快速連結

2014年11月19日

取得指定變數名字的數值

使用GetType().GetField().GetValue()可以輕鬆取得變數值TAT!



首先建立該物件。

public class Exmaple{
  public string name = "ExampleName":
  public List MyList = new List();
}





Example ex = new Example();
List outList = ex.GetType().GetField("MyList").GetValue(ex) as List;  // 將ex內的MyList值放入outList內!

如果在GetValue內放入null,則會出現Exception。
若為對象為靜態,則可以放入null。

沒有留言:

張貼留言

歡迎大家留言提問,我會答的都會盡力回答!
如果太久沒出現回應就是我又忘記回來看留言了TAT