调用:

     //转intvar int1 = "2".TryToInt();//转换为int失败返回0var int2 = "2x".TryToInt();var int3 = "2".TryToInt(1);//转换为int失败返回1var int4 = "2x".TryToInt(1);//转doublevar d1 = "2".TryToMoney(); //同上var d2 = "2x".TryToMoney();var d3 = "2".TryToMoney(1);var d4 = "2x".TryToMoney(1);//转stringstring a = null;var s1 = a.TryToString();var s3 = a.TryToString("1");//转decimalvar d11 = "2".TryToDecimal();var d22 = "2x".TryToDecimal();var d33 = "2".TryToDecimal(1);var d44 = "2x".TryToDecimal(1);//转datetimevar de1 = "2013-1-1".TryToDate();var de2 = "x2013-1-1".TryToDate();var de3 = "x2013-1-1".TryToDate(DateTime.Now);//json和model转换var json = new { id = 1 }.ModelToJson();var model = "{id:1}".JsonToModel<Model>();//list和dataTable转换var dt = new List<Model>() { new Model() { id = 1 } }.ListToDataTable();var list = dt.DataTableToList<Model>();

代码:(DataTable处做了修改)

    /// <summary>/// ** 描述:类型转换/// ** 创始时间:2015-6-2/// ** 修改时间:-/// ** 作者:sunkaixuan/// ** 使用说明:/// </summary>public static class TypeParseExtenions{#region 强转成int 如果失败返回 0/// <summary>/// 强转成int 如果失败返回 0/// </summary>/// <param name="thisValue"></param>/// <param name="i"></param>/// <returns></returns>public static int TryToInt(this object thisValue){int reval = 0;if (thisValue != null && int.TryParse(thisValue.ToString(), out reval)){return reval;}return reval;}#endregion#region 强转成int 如果失败返回 errorValue/// <summary>/// 强转成int 如果失败返回 errorValue/// </summary>/// <param name="thisValue"></param>/// <param name="i"></param>/// <returns></returns>public static int TryToInt(this object thisValue, int errorValue){int reval = 0;if (thisValue != null && int.TryParse(thisValue.ToString(), out reval)){return reval;}return errorValue;}#endregion#region 强转成double 如果失败返回 0/// <summary>/// 强转成money 如果失败返回 0/// </summary>/// <param name="thisValue"></param>/// <param name="i"></param>/// <returns></returns>public static double TryToMoney(this object thisValue){double reval = 0;if (thisValue != null && double.TryParse(thisValue.ToString(), out reval)){return reval;}return 0;}#endregion#region 强转成double 如果失败返回 errorValue/// <summary>/// 强转成double 如果失败返回 errorValue/// </summary>/// <param name="thisValue"></param>/// <param name="errorValue"></param>/// <returns></returns>public static double TryToMoney(this object thisValue, int errorValue){double reval = 0;if (thisValue != null && double.TryParse(thisValue.ToString(), out reval)){return reval;}return errorValue;}#endregion#region 强转成string 如果失败返回 ""/// <summary>/// 强转成string 如果失败返回 ""/// </summary>/// <param name="thisValue"></param>/// <param name="i"></param>/// <returns></returns>public static string TryToString(this object thisValue){if (thisValue != null) return thisValue.ToString().Trim();return "";}#endregion#region  强转成string 如果失败返回 errorValue/// <summary>/// 强转成string 如果失败返回 str/// </summary>/// <param name="thisValue"></param>/// <param name="errorValue"></param>/// <returns></returns>public static string TryToString(this object thisValue, string errorValue){if (thisValue != null) return thisValue.ToString().Trim();return errorValue;}#endregion#region 强转成Decimal 如果失败返回 0/// <summary>/// 强转成Decimal 如果失败返回 0/// </summary>/// <param name="thisValue"></param>/// <param name="i"></param>/// <returns></returns>public static Decimal TryToDecimal(this object thisValue){Decimal reval = 0;if (thisValue != null && decimal.TryParse(thisValue.ToString(), out reval)){return reval;}return 0;}#endregion#region 强转成Decimal 如果失败返回 errorValue/// <summary>/// 强转成Decimal 如果失败返回 errorValue/// </summary>/// <param name="thisValue"></param>/// <param name="errorValue"></param>/// <returns></returns>public static Decimal TryToDecimal(this object thisValue, int errorValue){Decimal reval = 0;if (thisValue != null && decimal.TryParse(thisValue.ToString(), out reval)){return reval;}return errorValue;}#endregion#region 强转成DateTime 如果失败返回 DateTime.MinValue/// <summary>/// 强转成DateTime 如果失败返回 DateTime.MinValue/// </summary>/// <param name="thisValue"></param>/// <param name="i"></param>/// <returns></returns>public static DateTime TryToDate(this object thisValue){DateTime reval = DateTime.MinValue;if (thisValue != null && DateTime.TryParse(thisValue.ToString(), out reval)){return reval;}return reval;}#endregion#region 强转成DateTime 如果失败返回 errorValue/// <summary>/// 强转成DateTime 如果失败返回 errorValue/// </summary>/// <param name="thisValue"></param>/// <param name="errorValue"></param>/// <returns></returns>public static DateTime TryToDate(this object thisValue, DateTime errorValue){DateTime reval = DateTime.MinValue;if (thisValue != null && DateTime.TryParse(thisValue.ToString(), out reval)){return reval;}return errorValue;}#endregion#region 将json序列化为实体/// <summary>/// 将json序列化为实体/// </summary>/// <typeparam name="TEntity"></typeparam>/// <param name="json"></param>/// <returns></returns>public static TEntity JsonToModel<TEntity>(this string json){JavaScriptSerializer jsSerializer = new JavaScriptSerializer();return jsSerializer.Deserialize<TEntity>(json);}/// <summary>/// 将实体序列化为json/// </summary>/// <param name="model"></param>/// <returns></returns>public static string ModelToJson<T>(this T model){JavaScriptSerializer jsSerializer = new JavaScriptSerializer();return jsSerializer.Serialize(model);}#endregion#region 将集合类list转换成DataTable/// <summary>/// 将集合类list转换成DataTable/// </summary>/// <param name="list">集合</param>/// <returns></returns>public static DataTable ListToDataTable<T>(this List<T> list){DataTable result = new DataTable();if (list.Count > 0){PropertyInfo[] propertys = typeof(T).GetProperties();foreach (PropertyInfo pi in propertys){result.Columns.Add(pi.Name, pi.PropertyType);}for (int i = 0; i < list.Count; i++){ArrayList tempList = new ArrayList();foreach (PropertyInfo pi in propertys){object obj = pi.GetValue(list[i], null);tempList.Add(obj);}object[] array = tempList.ToArray();result.LoadDataRow(array, true);}}return result;}#endregion#region  将DataTable转为集合类list/// <summary>/// 将datatable转为list/// </summary>/// <typeparam name="T"></typeparam>/// <param name="dt"></param>/// <returns></returns>public static List<T> DataTableToList<T>(this DataTable dt){var list = new List<T>();Type t = typeof(T);var plist = new List<PropertyInfo>(typeof(T).GetProperties());foreach (DataRow item in dt.Rows){T s = System.Activator.CreateInstance<T>();for (int i = 0; i < dt.Columns.Count; i++){PropertyInfo info = plist.Find(p => p.Name == dt.Columns[i].ColumnName);if (info != null){if (!Convert.IsDBNull(item[i])){info.SetValue(s, item[i], null);}}}list.Add(s);}return list;}#endregion}


文章出自:http://www.cnblogs.com/sunkaixuan/p/4548028.html

转载于:https://www.cnblogs.com/cang12138/p/5774064.html

通用扩展函数--类型转换相关推荐

  1. 29.rust类型转换.rs

    /* 1.基本类型可以通过显示类型转换机制(as)来实现相互之间的转换. 2.Rust通过使用trait来处理定制类型(enum.struct)之间的类型转换. 3.通用的类型转换一般使用的trait ...

  2. 注解形式控制器 数据验证,类型转换

    7.1.简介 在编写可视化界面项目时,我们通常需要对数据进行类型转换.验证及格式化. 一.在Spring3之前,我们使用如下架构进行类型转换.验证及格式化: 流程: ①:类型转换:首先调用Proper ...

  3. 《Spring 5 官方文档》5. 验证、数据绑定和类型转换(二)

    5.5 Spring类型转换 Spring 3引入了core.convert包来提供一个一般类型的转换系统.这个系统定义了实现类型转换逻辑的服务提供接口(SPI)以及在运行时执行类型转换的API.在S ...

  4. C++类型转换运算符(dynamic_cast, const_cast, static_cast)

    C++类型转换运算符 1. dynamic_cast 2. const_cast 3. static_cast 4. reinterpret_cast 摘自以下图书: <C++ Primer P ...

  5. C++之dynamic_cast、static_cast、const_cast、reinterpret_cast的用法和区别

    简述 C++中有四个类型转换符,旨在严格限制允许的类型转换,使转换更规范.这四个转换符分别是: dynamic_cast static_cast const_cast reinterpret_cast ...

  6. java上转型对象特点_Java 浅析三大特性之一继承

    上文Java 浅析三大特性之一封装我们说到Java是一个注重编写类,注重于代码和功能复用的语言.Java实现代码复用的方式有很多,这里介绍一个重要的复用方式--继承. 在介绍继承之前,我们要明确一点, ...

  7. Spring Framework 官方文档学习(四)之Validation、Data Binding、Type Conversion(二)

    接前一篇 Spring Framework 官方文档学习(四)之Validation.Data Binding.Type Conversion(一) 本篇主要内容:Spring Type Conver ...

  8. 《Kotlin 程序设计》第十二章 Kotlin的多线程:协程(Coroutines)

    第十二章 Kotlin的多线程:协程(Coroutines) Kotlin 1.1 introduced coroutines, a new way of writing asynchronous, ...

  9. Spring Framework 官方文档学习(四)之Validation、Data Binding、Type Conversion

    本篇太乱,请移步: Spring Framework 官方文档学习(四)之Validation.Data Binding.Type Conversion(一) 写了删删了写,反复几次,对自己的描述很不 ...

最新文章

  1. Spring Boot 配置加载顺序详解
  2. 在 C++ 中实现一个轻量的标记清除 gc 系统
  3. 创新式开发探索(一) —— 开篇
  4. 牛客网_PAT乙级_1019旧键盘 (20)
  5. sqlalchemy基本类型
  6. matlab fair,matlab练习程序(加权最小二乘)
  7. 计算机专业代码834,这六所高校更改专业课目录,其中不乏985高校,多数改为联考!...
  8. yzmcms图片自适应代码_基于segment.js制作的非常有创意的分段式SVG文字动画特效...
  9. javascript 函数,数组,document.write()
  10. 如何防止 Safari 使用来自 iOS 15 和 macOS Monterey 的网站着色?
  11. 防止孩子使用计算机的软件,如何防止熊孩纸在电脑里乱装软件
  12. itunes卸载工具_iTunes卸载麻烦
  13. Ghostscript命令实践
  14. 医疗行业某集团公司财务系统迁移阿里云的案例分享
  15. 使用FileZilla下载Jason2和Jason3雷达高度计数据
  16. 我的第一台手提 | 关于你的第一台手提征文活动
  17. IP地址的认识(一)
  18. KB / KiB,MB / MiB,GB / GiB,… 的区别
  19. 2022第十一届PMO大会日程已定,将于8月13-14日和20-21日线上召开
  20. 十大算法之弗洛伊德算法

热门文章

  1. Asp.net控件之异同:HTML控件与Web服务器控件
  2. 使用Spring框架的好处(转帖)
  3. etcd分布式之服务发现需要
  4. shell实例第14讲:字符串截取的8种方法
  5. 嵌入式ARM启动代码的工作
  6. AP(无线访问接入点(WirelessAccessPoint))
  7. C语言问题,在位运算中,操作数每右移一位,其结果相当于什么?若左移1位,其结果相当于什么?
  8. 运算方法和运算部件二
  9. Nginx+Redis+Ehcache:大型高并发与高可用的三层缓存架构总结
  10. UI开发模式-容器模式