工作心得网

工作心得精彩分享
代码积累持续学习

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

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.ReadLine();
        }
    }

我们的缺点麻烦您能提出,谢谢支持!

关于站长