c#实用工具方法——壹

        /// 
        /// 字典转key=value
        /// 
        /// 
        /// 
        public static string GetParamSrc(Dictionary paramsMap)
        {
            var vDic = from objDic in paramsMap orderby objDic.Key ascending select objDic;
            var str = new StringBuilder();
            foreach (var kv in vDic)
            {
                string pkey = kv.Key;
                string pvalue = kv.Value.ToString();
                str.Append(pkey + "=" + pvalue + "&");
            }
            var result = str.ToString().Substring(0, str.ToString().Length - 1);
            return result;
        }

        /// 
        /// 对象转换为字典
        /// 
        /// 待转化的对象
        /// 是否忽略NULL 这里我不需要转化NULL的值,正常使用可以不穿参数 默认全转换
        /// 
        public static Dictionary ObjectToMap(object obj, bool isIgnoreNull = false)
        {
            var map = new Dictionary();

            var t = obj.GetType(); // 获取对象对应的类, 对应的类型

            var pi = t.GetProperties(BindingFlags.Public | BindingFlags.Instance); // 获取当前type公共属性

            foreach (var p in pi)
            {
                var m = p.GetGetMethod();

                if (m != null && m.IsPublic)
                {
                    // 进行判NULL处理 
                    if (m.Invoke(obj, new object[] { }) != null || !isIgnoreNull)
                    {
                        map.Add(p.Name, m.Invoke(obj, new object[] { })); // 向字典添加元素
                    }
                }
            }
            return map;
        }

        /// 
        /// 获取时间戳
        /// 
        /// 
        public static long GetTimeSpan()
        {
            var startTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1)); // 当地时区
            var timeStamp = (long)(DateTime.Now - startTime).TotalSeconds; // 相差秒数
            return timeStamp;
        }

        /// 
        /// Unicode转字符串
        /// 
        /// 经过Unicode编码的字符串
        /// 正常字符串
        public static string Unicode2String(string source)
        {
            return new Regex(@"\\u([0-9A-F]{4})", RegexOptions.IgnoreCase | RegexOptions.Compiled).Replace(
                         source, x => string.Empty + Convert.ToChar(Convert.ToUInt16(x.Result("$1"), 16)));
        }

        /// 
        /// 对象转化为key=value
        /// 
        /// 
        /// 
        public static string SerializeFormData(object obj)
        {
            if (obj == null) return null;
            var froms = new System.Text.StringBuilder();
            var pis = obj.GetType().GetProperties();
            foreach (var pi in pis)
            {
                var provalue = pi.GetValue(obj, new object[] { });
                var fromvalue = System.Web.HttpUtility.UrlEncode(string.Format("{0}", GetValue(pi.PropertyType, provalue)));
                froms.AppendFormat("{0}={1}&", pi.Name, fromvalue);
            }
            if (froms.Length > 0)
            {
                froms.Remove(froms.Length - 1, 1);
            }
            return froms.ToString();
        }

 

你可能感兴趣的:(.net技术)