所以我有一個名爲PaypalTransaction的對象這裏是開始它,不需要顯示所有屬性來解釋問題。將屬性值添加到對象從字典
public class PaypalTransaction
{
public string first_name { get; set; }
public string last_name { get; set; }
public string custom { get; set; }
public string payer_email { get; set; }
....
....
}
現在我的問題是,我有一個foreach循環具有每個鍵作爲一個字符串
PaypalTransaction trans = new PaypalTransaction();
foreach(string key in keys)
{
// key = "first_name" , or "last_name , or "custom"
// how would I set the value of trans based on each key
// so when key = "first_name , I want to set trans.first_name
// something like trans.PropName[key].Value =
// I know that code isn't real , but with reflection i know this is possible
}
使用反射或簡單地使用'Dictionary' –