C#遍历类的属性,然后给其赋值

 1 public class PP
 2     {
 3         public string a { get; set; }
 4         public string b { get; set; }
 5         public string c { get; set; }
 6     }
 7     class Program
 8     {
 9         static void Main(string[] args)
10         {
11             Hashtable ht = new Hashtable();
12 
13 
14             ht.Add("a", "utf8");
15             ht.Add("b", "xxxx");
16             ht.Add("c", "xxxx");
17             PP config = new PP();
18             PropertyInfo[] propertys = config.GetType().GetProperties();
19             foreach (PropertyInfo property in propertys)
20             {
21                 for (int i = 0; i < ht.Count; i++)
22                 {
23                     property.SetValue(config, ht[property.Name].ToString(), null);
24                 }
25             }
26             Console.WriteLine(config.a+"\t"+config.b);
27             Console.ReadLine();
28         }
29     }

 

posted @ 2017-05-09 15:59  杨浪  阅读(5632)  评论(0编辑  收藏  举报