C#扩展方法类库StringExtensions
来源:互联网 发布:香港阿里云 淘宝 编辑:程序博客网 时间:2024/04/29 14:38
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(); }}
0 0
- C#扩展方法类库StringExtensions
- C# 类扩展方法
- C# 类的扩展方法
- C# 扩展类的方法
- C#扩展方法集合类
- C#扩展类的另一种方式:扩展方法
- C#中扩展类和扩展方法的使用
- C# 扩展系统类string的方法
- C# 类、结构与扩展方法
- C# 扩展系统类string的方法
- unity/C#中扩展方法类
- c#中的扩展方法
- C# 扩展方法
- C#扩展方法本质
- C#扩展方法
- 关于C#扩展方法
- C#之扩展方法
- C#2008扩展方法
- sgu121:Bridges painting
- NOIP复习计划
- Android Launcher开发(二)AppWidget(桌面小部件)解析
- js学习心得2
- struts2-dojo-plugin-2.3.1.2.jar!/struts-plugin.xml:29:119
- C#扩展方法类库StringExtensions
- Android Launcher开发(三)动态壁纸(LiveWallPaper)气泡流动效果
- DOM4J介绍与代码示例
- (转)CoreData教程
- iOS 各种网络编程面试总结
- Android Launcher开发(四)Launcher概述及简单添加和删除应用程序的快捷方式
- 系统系统!!!
- C++中list的erase()函数问题
- 1、任意输入一个4位正整数,调用函数(函数自定义)输出该正整数数的各位数 字组成的最大数。