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

Posted 懒懒的小易

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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();
        }



    }

 

以上是关于C# 自己用到的几个参数转换方法的主要内容,如果未能解决你的问题,请参考以下文章

C#中String类的几个方法(IndexOfLastIndexOfSubstring)

极端恢复时用到的几个隐藏参数

常用的几个JQuery代码片段

C#程序员经常用到的10个实用代码片段 - 操作系统

C#程序员经常用到的10个实用代码片段

自己写的几个常用到的函数