/// </summary>
        /// <typeparam name="T">泛型</typeparam>
        /// <param name="t">待转换类</param>
        /// <returns>参数拼接结果</returns>
        public static string GetSignParams<T>(T t)
        {
            OrdinalComparer comp = new OrdinalComparer();

            // 定义返回结果
            StringBuilder sb = new StringBuilder();
            // 取得属性集合
            PropertyInfo[] properties = t.GetType().GetProperties();
            // 升序排列
            properties = properties.OrderBy(p => p.Name, comp).ToArray();
            // 遍历属性
            foreach (PropertyInfo info in properties)
            {
                if (info.GetValue(t, null) == null)
                {
                    continue;
                }

                // 当前属性值不为空的情况
                if (!string.IsNullOrEmpty(info.GetValue(t, null).ToString()))
                {
                    // 拼接参数集合
                    sb.Append(info.Name).Append("=").Append(info.GetValue(t, null));
                }
            }
            return sb.ToString();
        }

排序用
   /// <summary>
        ///  获取实体属性和值并pinjie
 /// <summary>
        ///  自定义比较规则
        /// </summary>
        public class OrdinalComparer : IComparer<String>
        {
            public int Compare(String x, String y)
            {
                return string.CompareOrdinal(x, y);
            }
        }





本文转载:CSDN博客