zoukankan      html  css  js  c++  java
  • c# 常用 Common

            /// <summary>
            ///  md5加密字符串
            /// </summary>
            /// <param name="message"></param>
            /// <returns></returns>
            public static String ToMD5(this String message)
            {
                MD5 md5 = new MD5CryptoServiceProvider();
                byte[] result = Encoding.UTF8.GetBytes(message);
                byte[] outStr = md5.ComputeHash(result);
                string md5string = BitConverter.ToString(outStr).Replace("-", "");
                return md5string;
            }
            /// <summary>
            /// 按字节数截取字符串的方法
            /// </summary>
            /// <param name="source">要截取的字符串(可空)</param>
            /// <param name="NumberOfBytes">要截取的字节数</param>
            /// <param name="encoding">System.Text.Encoding</param>
            /// <param name="suffix">结果字符串的后缀(超出部分显示为该后缀)</param>
            /// <returns></returns>
            public static string SubStringByBytes(this string source, int NumberOfBytes, System.Text.Encoding encoding, string suffix = "")
            {
                if (string.IsNullOrWhiteSpace(source) || source.Length == 0)
                    return source;
    
                if (encoding.GetBytes(source).Length <= NumberOfBytes)
                    return source;
    
                long tempLen = 0;
                StringBuilder sb = new StringBuilder();
                foreach (var c in source)
                {
                    Char[] _charArr = new Char[] { c };
                    byte[] _charBytes = encoding.GetBytes(_charArr);
                    if ((tempLen + _charBytes.Length) > NumberOfBytes)
                    {
                        if (!string.IsNullOrWhiteSpace(suffix))
                            sb.Append(suffix);
                        break;
                    }
                    else
                    {
                        tempLen += _charBytes.Length;
                        sb.Append(encoding.GetString(_charBytes));
                    }
                }
                return sb.ToString();
            }
            /// <summary>
            /// 复制一个对象
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="info"></param>
            /// <returns></returns>
            public static T Copy<T>(T info) where T : new()
            {
                string json = JsonHelper.SerializeObject(info);
                return JsonConvert.DeserializeObject<T>(json);
            }
            /// <summary>
            /// 对象转Json字符串
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="data"></param>
            /// <returns></returns>
            public static string ToJson<T>(T data) where T : new()
            {
                return JsonHelper.SerializeObject(data);
            }
            /// <summary>
            /// 实体类转Dictionary
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="info"></param>
            /// <returns></returns>
            public static Dictionary<string,string> ModelToDictionary<T>(this T info)
            {
                Dictionary<string, string> dic = new Dictionary<string, string>();
                foreach (PropertyInfo pro in typeof(T).GetProperties())
                {
                    string value = pro.GetValue(info, null)?.ToString();
                    string name = pro.Name;
                    if (string.IsNullOrEmpty(value)) continue;
                    if (name == "Type") continue;
                    dic.Add(name, value);
                }
                return dic;
            }
            /// <summary>
            /// string扩展 rgb 转SolidColorBrush
            /// </summary>
            /// <param name="rgb"></param>
            /// <returns></returns>
            public static System.Windows.Media.SolidColorBrush ToSolidColorBrush(this string rgb)
            {
                try
                {
                    return new System.Windows.Media.SolidColorBrush((System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString(rgb));
                }
                catch (Exception)
                {
                    return System.Windows.Media.Brushes.Red;
                }
            }

     示例:"#999999".ToSolidColorBrush()

    原文:https://www.cnblogs.com/zisai/p/11050749.html 

  • 相关阅读:
    23. 霍纳法则(多项式求值快速算法)
    22. 欧几里德算法(求最大公约数GCD)
    [poj 2106] Boolean Expressions 递归
    [poj 1185] 炮兵阵地 状压dp 位运算
    [MOOC程序设计与算法二] 递归二
    [poj 3254] Corn Fields 状压dp
    [hdu 1074] Doing Homework 状压dp
    [hdu 1568] Fibonacci数列前4位
    [haut] 1281: 邪能炸弹 dp
    [hdu 2604] Queuing 递推 矩阵快速幂
  • 原文地址:https://www.cnblogs.com/zisai/p/11050749.html
Copyright © 2011-2022 走看看