获得对象所有属性的值的拼接
/// <summary> /// 获得对象所有属性的值的拼接 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="t"></param> /// <returns></returns> public static Dictionary<string, string> GetPropertyValueString<T>(T t, List<string> signProperties) { Dictionary<string, string> values = new Dictionary<string, string>(); if (t == null) { return null; } System.Reflection.PropertyInfo[] properties = t.GetType().GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public); if (properties.Length <= 0) { return null; } foreach (System.Reflection.PropertyInfo item in properties) { string name = item.Name; if (signProperties != null && signProperties.Count > 0) { if (signProperties.Contains(name)) { object value = item.GetValue(t, null); if (item.PropertyType.IsValueType || item.PropertyType.Name.StartsWith("String")) { values.Add(name, value == null ? "" : value.ToString()); } } } else { object value = item.GetValue(t, null); if (item.PropertyType.IsValueType || item.PropertyType.Name.StartsWith("String")) { values.Add(name, value == null ? "" : value.ToString()); } } //object value = item.GetValue(t, null); //if (item.PropertyType.IsValueType || item.PropertyType.Name.StartsWith("String")) //{ // //tStr += string.Format("{0}:{1},", name, value); // values.Add(name, value == null ? "" : value.ToString()); //} ////else ////{ //// GetPropertyValueString(value);//对象中嵌套的对象 ////} } return values; }
签名字符串
public static string GetSignContent(IDictionary<string, string> parameters) { // 第一步:把字典按Key的字母顺序排序 IDictionary<string, string> sortedParams = new SortedDictionary<string, string>(parameters); IEnumerator<KeyValuePair<string, string>> dem = sortedParams.GetEnumerator(); // 第二步:把所有参数名和参数值串在一起 StringBuilder query = new StringBuilder(""); while (dem.MoveNext()) { string key = dem.Current.Key; string value = dem.Current.Value; if (!string.IsNullOrEmpty(key) && !string.IsNullOrEmpty(value)) { query.Append(key).Append("=").Append(value).Append("&"); } } string content = query.ToString().Substring(0, query.Length - 1); return content; }