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