根据类拼凑成url参数


        /// <summary>
        /// 根据类拼凑成url参数
        /// </summary>
        /// <typeparam name="T">类</typeparam>
        /// <param name="obj"></param>
        /// <param name="firstStr">第一个字符 ?或者&  </param>
        /// <returns>如: ?action=get&data=123</returns>
        public static string GetUrlParas<T>(T obj, string firstStr = "?") where T : class //限制为类
        {
            return obj.GetType().GetProperties().Aggregate(firstStr, (current, p) => current + string.Format("{0}={1}&", p.Name, p.GetValue(obj, null) ?? "")).Trim('&');
        }


posted @ 2018-06-13 11:38  change_4_now  阅读(109)  评论(0编辑  收藏  举报