• 00
  • :
  • 00
  • :
  • 00

反射获得类属性

protected void AutoSetParam<TI>(IRfcDataContainer container, TI model)
{
	Type t = model.GetType();
	PropertyInfo[] PropertyList = t.GetProperties(BindingFlags.Instance | BindingFlags.Public);
	foreach (PropertyInfo item in PropertyList)
	{
		string name = item.Name;
		if (name.Substring(0, 1) == "_") continue;
		object value = item.GetValue(model, null);
		container.SetValue(name.ToUpper(), value);
	}
}

  带自定义属性

Type t = HeaderData.GetType();
                PropertyInfo[] PropertyList = t.GetProperties(BindingFlags.Instance | BindingFlags.Public);
                foreach (PropertyInfo item in PropertyList)
                {

                    string name = item.Name;
                    var attr = item.GetCustomAttribute<DisplayNameAttribute>();
                    if (attr != null)
                    {
                        name = attr.DisplayName;
                    }
                    object value = item.GetValue(HeaderData, null);
                    request.Headers.Add(name, value.ToString());
                }

  

posted @ 2020-05-08 21:36  Garson_Zhang  阅读(181)  评论(0编辑  收藏  举报