遍历class的所有属性,给属性赋值

 public class PP
    {
        public string a { get; set; }
        public string b { get; set; }
        public string c { get; set; }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Hashtable ht = new Hashtable();


            ht.Add("a", "utf8");
            ht.Add("b", "xxxx");
            ht.Add("c", "xxxx");
            PP config = new PP();
            PropertyInfo[] propertys = config.GetType().GetProperties();
            foreach (PropertyInfo property in propertys)
            {
                for (int i = 0; i < ht.Count; i++)
                {
                    property.SetValue(config, ht[property.Name].ToString(), null);
                }
            }
            Console.WriteLine(config.a + "\t" + config.b);

            Console.WriteLine("PP.a =" +config.a);
        }
    }

 

posted @ 2018-07-24 15:54  blog_yuan  阅读(434)  评论(0)    收藏  举报