using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Globalization;
using System.IO;
using System.Web;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Reflection;
using ECS.Utility;

public static class StringExtensions
{
    public static void BindEnumList(this CheckBoxList ddl, Type obj)
    {
        if (!obj.IsEnum)
            throw new Exception("value not enum!");

        var itemArr = Enum.GetValues(obj);

        foreach (var item in itemArr)
        {
            ddl.Items.Add(new ListItem(item.ToString(), ((int)item).ToString()));
        }
    }

    public static void BindEnumList(this DropDownList ddl, Type obj)
    {
        if (!obj.IsEnum)
            throw new Exception("value not enum!");

        var itemArr = Enum.GetValues(obj);

        foreach (var item in itemArr)
        {
            ddl.Items.Add(new ListItem(item.ToString(), ((int)item).ToString()));
        }
    }

    public static void BindEnumDescriptionList(this DropDownList ddl, Type obj)
    {
        if (!obj.IsEnum)
        {
            throw new ArgumentException("enumItem requires a Enum ");
        }

        var itemArr = Enum.GetValues(obj);
        string[] names = Enum.GetNames(obj);
        FieldInfo fieldInfo;
        object[] attributes;
        DescriptionAttribute descriptionAttribute;

        foreach (string name in names)
        {
            fieldInfo = obj.GetField(name);
            attributes = fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);
            var value = (int)fieldInfo.GetValue(typeof(string));
            if (attributes.Length > 0)
            {
                descriptionAttribute = attributes.First() as DescriptionAttribute;
                if (descriptionAttribute != null)
                {
                    ddl.Items.Add(new ListItem(descriptionAttribute.Description, value.ToString()));
                }
            }
        }
    }

    public static int ToInt(this string value)
    {
        return Int32.Parse(value);
    }

    public static int ToInt(this string value, int defaultValue)
    {
        var result = defaultValue;
        return int.TryParse(value, out result) ? result : defaultValue;
    }

    public static int? ToNullableInt(this string value)
    {
        int result;

        if (string.IsNullOrEmpty(value) || !int.TryParse(value, out result))
        {
            return null;
        }

        return result;
    }

    public static decimal ToDecimal(this string value)
    {
        return decimal.Parse(value);
    }

    public static decimal ToDecimal(this string value, decimal defaultValue)
    {
        var result = defaultValue;
        return decimal.TryParse(value, out result) ? result : defaultValue;
    }

    public static decimal ToRoundDecimal(this string value, decimal defaultValue, int decimals)
    {
        var result = defaultValue;
        result = Math.Round(decimal.TryParse(value, out result) ? result : defaultValue, decimals);
        return result;
    }

    public static decimal? ToNullableDecimal(this string value)
    {
        decimal result;
        if (string.IsNullOrEmpty(value) || !decimal.TryParse(value, out result))
        {
            return null;
        }
        return result;
    }

    public static short? ToNullableShort(this string value)
    {
        short result;

        if (string.IsNullOrEmpty(value) || !short.TryParse(value, out result))
        {
            return null;
        }

        return result;
    }

    public static DateTime? ToNullableDateTime(this string value)
    {
        DateTime result;

        if (DateTime.TryParse(value, out result))
        {
            return result;
        }

        return null;
    }

    public static DateTime ToDateTime(this string value)
    {
        return DateTime.Parse(value);
    }

    public static byte? ToNullableByte(this string value)
    {
        byte result;

        if (string.IsNullOrEmpty(value) || !byte.TryParse(value, out result))
        {
            return null;
        }

        return result;
    }

    public static bool? ToNullableBool(this string value)
    {
        bool result;

        if (string.IsNullOrEmpty(value) || !bool.TryParse(value, out result))
        {
            return null;
        }

        return result;
    }

    public static bool ToBool(this string value)
    {
        return bool.Parse(value);
    }

    /// <summary>
    /// 去掉字符串中的html
    /// </summary>
    /// <param name="value"></param>
    /// <returns></returns>
    public static string ToNoHtmlString(this string value)
    {
        return Util.StripHTML(value).Trim();
    }

}

C#扩展方法类库StringExtensions的更多相关文章

  1. 开源且功能强大的C# 扩展方法类库Pure.Ext,包含1000+个拓展方法 (支持.Net Framework和.Net Core)

    先上地址 Github: https://github.com/purestackorg/pure.ext Gitee: https://gitee.com/purestack/pure.ext 扩展 ...

  2. C#秘密武器之扩展方法

    原文:C#秘密武器之扩展方法 为何要用扩展方法? 作为一个.NET程序猿,我们经常要跟.net自带类库或者第三方dll类库打交道,有时候我们未必能够通过反编译来查看它们的代码,但是我们通常需要给它们扩 ...

  3. 【原创】开源Math.NET基础数学类库使用(12)C#随机数扩展方法

                   本博客所有文章分类的总目录:[总目录]本博客博文总目录-实时更新  开源Math.NET基础数学类库使用总目录:[目录]开源Math.NET基础数学类库使用总目录 前言 ...

  4. 开源Math.NET基础数学类库使用(12)C#随机数扩展方法

    原文:[原创]开源Math.NET基础数学类库使用(12)C#随机数扩展方法                本博客所有文章分类的总目录:http://www.cnblogs.com/asxinyu/p ...

  5. 【开源】OSharp框架解说系列(3):扩展方法

    OSharp是什么? OSharp是个快速开发框架,但不是一个大而全的包罗万象的框架,严格的说,OSharp中什么都没有实现.与其他大而全的框架最大的不同点,就是OSharp只做抽象封装,不做实现.依 ...

  6. .NET 扩展方法 (一)

    我还记得刚刚学编程的时候,老师经常会提到一句话:注意空指针.所以经常在某些“入口”位置,进行代码校验,空指针的判断就是其中的一项工作. string类型作为常用的数据类型,它在项目中出现的机率极高,所 ...

  7. .NET 扩展方法 (二)

    上一篇随笔 .NET 扩展方法 (一) 已经对 扩展方法有了大致的介绍,这篇算是一个补充,让我们来看一下扩展方法的几个细节: 一.扩展方法具有继承性 当使用扩展方法扩展一个类型的时候,其也扩展了派生类 ...

  8. 再谈扩展方法,从string.IsNullOrEmpty()说起

    string.IsNullOrEmpty()这个方法算得上是.net中使用频率最高的方法之一.此方法是string的一个静态方法,类似的静态方法在string这个类中还有很多.那么这样的方法作为静态方 ...

  9. .NET开发中经常用到的扩展方法

    整理一下自己经常用到的几个扩展方法,在实际项目中确实好用,节省了不少的工作量. 1  匿名对象转化 在WinForm中,如果涉及较长时间的操作,我们一般会用一个BackgroundWorker来做封装 ...

随机推荐

  1. HDU [P2819] swap

    二分图行列匹配+输出路径 经典题,当且仅当一行匹配一列的时候,符合题意. 本题的难点在于如何输出路径,我们发现这个移动的过程就是将所有匹配选择排序,在选择排序时输出路径即可 #include < ...

  2. BZOJ 2024: [SHOI2009] 舞会 [容斥原理 高精度]

    题意:和上题基本一样,求至少k对a>b的方案数.不取模!!! 做k+1遍容斥就行了 高精度超强!!!几乎把所有的都用上了 然后,注意有负数,所以容斥的时候正负分别保存然后再一减就行了 这是我省选 ...

  3. BZOJ 1492: [NOI2007]货币兑换Cash [CDQ分治 斜率优化DP]

    传送门 题意:不想写... 扔链接就跑 好吧我回来了 首先发现每次兑换一定是全部兑换,因为你兑换说明有利可图,是为了后面的某一天两种卷的汇率差别明显而兑换 那么一定拿全利啊,一定比多天的组合好 $f[ ...

  4. 有关static静态方法知识的收集

    1.何时使用静态方法: 如果某些操作不依赖具体实例,那它就是静态的,反之如果某些操作是依赖具体实例的(例如访问一个特定会员的名称),那它就应该是实例化的. 2.静态方法和实例方法的区别主要体现在两个方 ...

  5. 树莓派3B上部署运行.net core 2程序

    针对Linxu arm处理器如何部署.net core 2的资料很少,网上找到几篇但都写得不够详细,按照他们教程来撞墙了,折磨了几天终于部署成功了,先上一张运行成功的图 1.windows系统中,在项 ...

  6. jquary 单选,多选,select 获取和设置值 jquary自定义函数

    <%@ page contentType="text/html; charset=UTF-8"%> <%@ taglib prefix="c" ...

  7. POI导出多张图片到Excel

    package com.sun.office.excel; import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStr ...

  8. gitlab手动安装

    [博客园 淡水的天空]] 老版 新版 Omnibus package installation Manually

  9. Centos7安装GitLab

    GitLab CE Download Archives gitlab安装调试小记 Gitlab Free Trial GitLab搭建手记 Gitlab社区版的使用 GUI PNG Gitlab升级到 ...

  10. 数据库之mac上mysql root密码忘记或权限错误的解决办法

    [转自  http://blog.csdn.net/u014410695/article/details/50630233] 以下方法亲测有效,过程使用的工具只有mac的终端无需workbench 当 ...