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();
        }
    }
發佈了40 篇原創文章 · 獲贊 13 · 訪問量 9萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章