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 

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