如何给实体属性自动赋值

发布时间 2023-04-18 11:39:23作者: 星辰与大海
namespace demo
{
	using System.Reflection;
	using System.Collections;

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

	public class PropertySetValue
	{
		public void Demo()
		{
			//将hashtable的key值赋给实体类config.
			Hashtable ht = new Hashtable();
			ht.Add("encoding", "utf-8");
			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);
				}
			}
		}

	}


}