public static SortedDictionary <string, string> GetObjPayDescriptionFormat <T>(T t) where T : new() { Type type = t.GetType(); PropertyInfo[] pinfos = type.GetProperties(); SortedDictionary <string, string> attrSD = new SortedDictionary <string, string>(); foreach (PropertyInfo p in pinfos) { object[] attr = p.GetCustomAttributes(typeof(PayDescriptionAttribute), true); if (attr.Length > 0) { PayDescriptionAttribute pda = attr[0] as PayDescriptionAttribute; object vobj = p.GetValue(t, null); string v = vobj == null ? string.Empty : vobj.ToString(); attrSD.Add(pda.Description, v); } } return(attrSD); }
/// <summary> /// 绑定Http请求对象到对象T中 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="t"></param> /// <param name="httpRequest">Http请求对象</param> /// <returns></returns> public static T GetObjPayDescriptionFormat <T>(NameValueCollection httpRequest) where T : new() { T t = System.Activator.CreateInstance <T>(); Type type = t.GetType(); PropertyInfo[] pinfos = type.GetProperties(); SortedDictionary <string, string> attrSD = new SortedDictionary <string, string>(); foreach (PropertyInfo p in pinfos) { object[] attr = p.GetCustomAttributes(typeof(PayDescriptionAttribute), true); if (attr.Length > 0) { PayDescriptionAttribute pda = attr[0] as PayDescriptionAttribute; string pvalue = httpRequest[pda.Description]; p.SetValue(t, pvalue, null); //设置属性新值 } } return(t); }