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);
}
}

发布了64 篇原创文章 · 获赞 15 · 访问量 7255

猜你喜欢

转载自blog.csdn.net/weixin_42263618/article/details/103718991