C# 拓展字符串,枚举类型

C# 拓展字符串


 public static class StringMethod
    {
        public static string SqlReplace(this string str)
        {
            string result = str;
            if (!string.IsNullOrEmpty(str))
            {
                result = str.Replace("'", "''");
            }
            return result;
        }

        public static int ToInt32(this string str)
        {
            // return Convert.ToInt32(str);
            int result = 0;
            if (!string.IsNullOrEmpty(str))
            {
                int.TryParse(str, out result);
            }
            return result;
        }

        public static bool ToBoolean(this string str)
        {
            bool result = true;
            if (!string.IsNullOrEmpty(str))
            {
                Boolean.TryParse(str, out result);
            }
            return result;
        }

        public static decimal ToDecimal(this string str)
        {
            // return Convert.ToDecimal(str);
            decimal result = Decimal.Zero;
            if (!string.IsNullOrEmpty(str))
            {
                decimal.TryParse(str, out result);
            }
            return result;
        }

        public static DateTime TryDateTime(this string str)
        {
            DateTime dt = DateTime.MinValue;
            if (!string.IsNullOrEmpty(str))
            {
                DateTime.TryParse(str, out dt);
            }
            dt = DateTime.SpecifyKind(dt, DateTimeKind.Utc);
            return dt;
        }

        public static DateTime TryDateTime(this string str, string type)
        {
            DateTime dt = DateTime.MinValue;
            if (type.ToLower() == "max100")
            {
                dt = Convert.ToDateTime("2111-1-1");
            }
            if (!string.IsNullOrEmpty(str))
            {
                DateTime.TryParse(str, out dt);
            }
            dt = DateTime.SpecifyKind(dt, DateTimeKind.Utc);
            return dt;
        }

        public static string SHA1Encrypt(this string str)
        {
            // sql2008 sha1加密 sys.fn_VarBinToHexStr(HashBytes('SHA1', str))
            byte[] strRes = Encoding.Default.GetBytes(str);
            HashAlgorithm iSHA = new SHA1CryptoServiceProvider();
            strRes = iSHA.ComputeHash(strRes);
            StringBuilder enText = new StringBuilder();
            foreach (byte iByte in strRes)
            {
                enText.AppendFormat("{0:x2}", iByte);
            }
            return enText.ToString();
        }
    }

C# 拓展枚举

首先定义一个枚举,描述中写明字符串的值

  public enum Language
    {
        [DescriptionAttribute("")]
        Empty,

        [DescriptionAttribute("en")]
        English,

        [DescriptionAttribute("zh-CHS")]
        SimplifiedChinese,

        [DescriptionAttribute("zh-CHT")]
        TraditionalChinese,

        [DescriptionAttribute("ko-KR")]
        Korean,
    }



给枚举拓展一个方法,用于获取枚举的字符串值

    public static class EnumMethod
    {
        public static string StringValue(this Enum value)
        {
            FieldInfo fi = value.GetType().GetField(value.ToString());
            DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
            if (attributes.Length > 0)
            {
                return attributes[0].Description;
            }
            else
            {
                return value.ToString();
            }
        }
    }

使用示例


Language language;

language.StringValue().SqlReplace()


你可能感兴趣的:(C# 拓展字符串,枚举类型)