string to variable name

前端 未结 4 518
一向
一向 2020-11-27 05:50

I have class(Customer) which holds more than 200 string variables as property. I\'m using method with parameter of key and value. I trying to supply key and value from xml f

4条回答
  •  臣服心动
    2020-11-27 06:14

    Use reflection and a dictionary object as your items collection.

    Dictionary customerProps = new Dictionary();
    Customer myCustomer = new Customer(); //Or however you're getting a customer object
    
    foreach (PropertyInfo p in typeof(Customer).GetProperties())
    {
        customerProps.Add(p.Name, p.GetValue(customer, null));
    }
    

提交回复
热议问题