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

时间:2022-11-07 06:35:59
 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 = ; i < ht.Count; i++)
{
property.SetValue(config, ht[property.Name].ToString(), null);
}
}
Console.WriteLine(config.a+"\t"+config.b);
Console.ReadLine();
}
}