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     }

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM