• 一个很实用的StringExtension


    先放出源码:

    public static class StringExtensions
    {
        // Methods
        public static TValue As<TValue>(this string value)
        {
            return value.As<TValue>(default(TValue));
        }
    
        public static TValue As<TValue>(this string value, TValue defaultValue)
        {
            try
            {
                TypeConverter converter = TypeDescriptor.GetConverter(typeof(TValue));
                if (converter.CanConvertFrom(typeof(string)))
                {
                    return (TValue) converter.ConvertFrom(value);
                }
                converter = TypeDescriptor.GetConverter(typeof(string));
                if (converter.CanConvertTo(typeof(TValue)))
                {
                    return (TValue) converter.ConvertTo(value, typeof(TValue));
                }
            }
            catch (Exception)
            {
            }
            return defaultValue;
        }
    
        public static bool AsBool(this string value)
        {
            return value.As<bool>(false);
        }
    
        public static bool AsBool(this string value, bool defaultValue)
        {
            return value.As<bool>(defaultValue);
        }
    
        public static DateTime AsDateTime(this string value)
        {
            return value.As<DateTime>();
        }
    
        public static DateTime AsDateTime(this string value, DateTime defaultValue)
        {
            return value.As<DateTime>(defaultValue);
        }
    
        public static decimal AsDecimal(this string value)
        {
            return value.As<decimal>();
        }
    
        public static decimal AsDecimal(this string value, decimal defaultValue)
        {
            return value.As<decimal>(defaultValue);
        }
    
        public static float AsFloat(this string value)
        {
            return value.As<float>();
        }
    
        public static float AsFloat(this string value, float defaultValue)
        {
            return value.As<float>(defaultValue);
        }
    
        public static int AsInt(this string value)
        {
            return value.As<int>();
        }
    
        public static int AsInt(this string value, int defaultValue)
        {
            return value.As<int>(defaultValue);
        }
    
        public static bool Is<TValue>(this string value)
        {
            TypeConverter converter = TypeDescriptor.GetConverter(typeof(TValue));
            return (((converter != null) && converter.CanConvertFrom(typeof(string))) && converter.IsValid(value));
        }
    
        public static bool IsBool(this string value)
        {
            return value.Is<bool>();
        }
    
        public static bool IsDateTime(this string value)
        {
            return value.Is<DateTime>();
        }
    
        public static bool IsDecimal(this string value)
        {
            return value.Is<decimal>();
        }
    
        public static bool IsEmpty(this string value)
        {
            return string.IsNullOrEmpty(value);
        }
    
        public static bool IsFloat(this string value)
        {
            return value.Is<float>();
        }
    
        public static bool IsInt(this string value)
        {
            return value.Is<int>();
        }
    }
    其实,这个string扩展是微软Asp.net webpages 开发中的一个辅助类,但是现在的项目没有用到asp.net webpages相关开发及其razor;该类存在于System.Web.WebPages中,如果直接引用该程序集,焉是杀鸡用牛刀?

    image

    如果自己写一个string扩展类也不难,但是我是直接通过reflector反编译过来借用了。

    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    洛谷P3455
    开发人员的奋斗目标
    js判读周末以及节假日
    c#中集成Swagger
    Combo Select – jQuery可搜索下拉框插件
    接口对接 调用与处理方式
    问题集锦
    sql server 自定义函数的使用
    Api接口服务的设计和安全解决方案
    使用Jquery Ajax请求 下载压缩文件
  • 原文地址:https://www.cnblogs.com/ecin/p/2423815.html
Copyright © 2020-2023  润新知