C#遍歷實體的所有屬性並為屬性賦值



   using System.Reflection;

 

           class Config
           {
                public string encoding { get; set; }
                public string plugins { get; set; }

           } 

 

           將hashtable的key值賦給實體類config. 

       

             Hashtable ht = new Hashtable();

            ht.Add("encoding", "utf8");
            ht.Add("plugins", "xxxx");
            Config config = new Config();
            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);
                }
            }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM