C# 自己用到的几个参数转换方法

 /// <summary>
    /// Method:CommandHelper
    /// Author:Liuyangyi
    /// Data:2016-05-10
    /// </summary>
    public class Command
    {

        /// 提供标准 MD5 加密的方法
        /// </summary>
        /// <param name="cort"></param>
        /// <returns></returns>
        /// <summary>
        /// 标准的md5密码加密算法
        /// </summary>
        /// <param name="toCryString"></param>
        /// <returns></returns>
        public static string Ext_GetStandMD5(string toCryString)
        {
            MD5CryptoServiceProvider hashmd5;
            hashmd5 = new MD5CryptoServiceProvider();
            return BitConverter.ToString(hashmd5.ComputeHash(Encoding.Default.GetBytes(toCryString.Trim()))).Replace("-", "").ToUpper();
        }
        /// <summary>
        /// Unix时间戳
        /// </summary>
        /// <param name="dateTime"></param>
        /// <returns></returns>
        public static string DateTimeToUnixTimestamp()
        {
            DateTime dateTime = DateTime.Now;
            var start = new DateTime(1970, 1, 1, 0, 0, 0, dateTime.Kind);
            return Convert.ToInt64((dateTime - start).TotalSeconds).ToString();
        }

        /// <summary>
        /// 时间格式
        /// </summary>
        /// <returns></returns>
        public static string DateTimestamp()
        {
            DateTime dateTime = DateTime.Now;
            return dateTime.ToString("yyyy-MM-dd HH:mm:ss");
        }
        /// <summary>
        /// 除去数组中的空值和签名参数并以字母a到z的顺序排序
        /// </summary>
        /// <param name="dicArrayPre">过滤前的参数组</param>
        /// <returns>过滤后的参数组</returns>
        public static Dictionary<string, object> FilterPara(SortedDictionary<string, object> dicArrayPre)
        {
            Dictionary<string, object> dicArray = new Dictionary<string, object>();
            foreach (KeyValuePair<string, object> temp in dicArrayPre)
            {
                if (temp.Key.ToLower() != "sign" && temp.Value != null && temp.Value != "")
                {
                    dicArray.Add(temp.Key, temp.Value);
                }
            }

            return dicArray;
        }

        /// <summary>
        /// 
        /// 排序加清除为空的参数
        /// </summary>
        /// <param name="o"></param>
        /// <returns></returns>
        public static Dictionary<String, Object> ToMap(Object o)
        {
            SortedDictionary<String, Object> map = new SortedDictionary<string, object>();

            Type t = o.GetType();

            PropertyInfo[] pi = t.GetProperties(BindingFlags.Public | BindingFlags.Instance);

            foreach (PropertyInfo p in pi)
            {
                MethodInfo mi = p.GetGetMethod();

                if (mi != null && mi.IsPublic)
                {
                    map.Add(p.Name, mi.Invoke(o, new Object[] { }));
                }
            }

            return FilterPara(map);

        }



        /// <summary>
        /// 把数组所有元素,按照“参数=参数值”的模式用“&”字符拼接成字符串
        /// </summary>
        /// <param name="dicArray"></param>
        /// <returns></returns>
        public static string CreateLinkString(Dictionary<string, object> dicArray)
        {
            StringBuilder prestr = new StringBuilder();
            foreach (KeyValuePair<string, object> temp in dicArray)
            {
                if (temp.Key.ToLower() != "sign" && temp.Key.ToLower() != "pid" && temp.Value != null && temp.Value != "")
                {
                    prestr.Append(temp.Key + "=" + temp.Value.ToString().Replace(" ", "") + "&");
                }
            }
            //去掉最後一個&字符
            int nLen = prestr.Length;
            prestr.Remove(nLen - 1, 1);

            return prestr.ToString();
        }



    }

 

posted @ 2016-05-20 12:57  懒懒的小易  阅读(650)  评论(0编辑  收藏  举报