基础延伸

 1  /// <summary>
 2         /// 对象深拷贝
 3         /// </summary>
 4         /// <typeparam name="T"></typeparam>
 5         /// <param name="obj"></param>
 6         /// <returns></returns>
 7         public static T DeepCopyByBin<T>(this T obj)
 8         {
 9             object retval;
10             using (MemoryStream ms = new MemoryStream())
11             {
12                 BinaryFormatter bf = new BinaryFormatter();
13                 //序列化成流
14                 bf.Serialize(ms, obj);
15                 ms.Seek(0, SeekOrigin.Begin);
16                 //反序列化成对象
17                 retval = bf.Deserialize(ms);
18                 ms.Close();
19             }
20             return (T)retval;
21         }

 

  /// <summary>
        /// 包含中文
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static bool ContainsChinese(this string input)
        {
            if (string.IsNullOrEmpty(input))
                return false;

            return Regex.IsMatch(input, "[\u4e00-\u9fa5]");
        }

 

   //获取描述特性
     public static string ToDescription(Enum en)
        {
            try
            {
                Type type = en.GetType();   //获取类型            
                MemberInfo[] memberInfos = type.GetMember(en.ToString());   //获取成员           
                if (memberInfos != null && memberInfos.Length > 0)
                {
                    DescriptionAttribute[] attrs = memberInfos[0].GetCustomAttributes(typeof(DescriptionAttribute), false) as DescriptionAttribute[];   //获取描述特性                 
                    if (attrs != null && attrs.Length > 0)
                    {
                        return attrs[0].Description;    //返回当前描述           
                    }
                }
                return en.ToString();
            }
            catch
            {
                return "Error Description";
            }
        }

 

posted @ 2023-02-21 11:14  代码MM  阅读(9)  评论(0编辑  收藏  举报