代码实现:

var int1 = "".TryToInt();//转换为int失败返回0
var int2 = "2x".TryToInt();
var int3 = "".TryToInt();//转换为int失败返回1
var int4 = "2x".TryToInt(); var d1 = "".TryToMoney(); //同上
var d2 = "2x".TryToMoney();
var d3 = "".TryToMoney();
var d4 = "2x".TryToMoney(); string a = null;
var s1 = a.TryToString();
var s3 = a.TryToString(""); var d11 = "".TryToDecimal();
var d22 = "2x".TryToDecimal();
var d33 = "".TryToDecimal();
var d44 = "2x".TryToDecimal(); var 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 = }.ModelToJson();
var model = "{id:1}".JsonToModel<ModelTest>(); //list和dataTable转换
var dt = new List<ModelTest>().ListToDataTable();
var list = dt.DataTableToList<ModelTest>();

封装类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Script.Serialization;
using System.Data;
using System.Reflection;
using System.Collections; namespace SyntacticSugar
{
/// <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 = ;
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 = ;
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 = ;
if (thisValue != null && double.TryParse(thisValue.ToString(), out reval))
{
return reval;
}
return ;
}
#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 = ;
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 = ;
if (thisValue != null && decimal.TryParse(thisValue.ToString(), out reval))
{
return reval;
}
return ;
}
#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 = ;
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 DataTable List
/// <summary>
/// 将集合类转换成DataTable
/// </summary>
/// <param name="list">集合</param>
/// <returns></returns>
public static DataTable ListToDataTable<T>(this List<T> list)
{
DataTable result = new DataTable();
if (list.Count > )
{
PropertyInfo[] propertys = typeof(T).GetProperties();
foreach (PropertyInfo pi in propertys)
{
result.Columns.Add(pi.Name, pi.PropertyType);
} for (int i = ; i < list.Count; i++)
{
ArrayList tempList = new ArrayList();
foreach (PropertyInfo pi in propertys)
{
object obj = pi.GetValue(list[i], null);
if (obj != null && obj != DBNull.Value)
tempList.Add(obj);
}
object[] array = tempList.ToArray();
result.LoadDataRow(array, true);
}
}
return result;
}
/// <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 = ; 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 }
}

通用扩展函数之TypeParse的更多相关文章

  1. ASP.NETC#通用扩展函数之TypeParse 类型转换方便多了

    用法: var int1 = "2".TryToInt();//转换为int失败返回0 var int2 = "2x".TryToInt(); var int3 ...

  2. C#ASP.NET 通用扩展函数之 LogicSugar 简单好用

    说明一下性能方面 还可以接受 循环1000次普通Switch是用了0.001秒 ,扩展函数为0.002秒  , 如果是大项目在有负载均衡的情况下完全可以无视掉,小项目也不会计较这点性能了. 注意需要引 ...

  3. C#ASP.NET 通用扩展函数之 IsWhat 简单好用

    好东西都需要人去整理.分类 注意:需要引用命名空间 SyntacticSugar 用法: /***扩展函数名细***/ //[IsInRange] int num = 100; //以前写法 if ( ...

  4. 功能齐全并且比较时髦的Jquery通用开源框架之【ejq.js】

    简介 ejq是一款非常小巧的JS工具库,未压缩才50K,在jquery的基础上对jquery缺失部分作了很好的弥补作用. 优点: 1.具有内置的模板解析引擎语法和angularjs相近减少学习成本 2 ...

  5. [转]Jquery通用开源框架之【ejq.js】

    ejq是一款非常小巧的JS工具库,未压缩才50K,在jquery的基础上对jquery缺失部分作了很好的弥补作用. 优点: 1.具有内置的模板解析引擎语法和angularjs相近减少学习成本 2.能够 ...

  6. query通用开源框架

    Jquery通用开源框架之[ejq.js] 简介 ejq是一款非常小巧的JS工具库,未压缩才50K,在jquery的基础上对jquery缺失部分作了很好的弥补作用. 优点: 1.具有内置的模板解析引擎 ...

  7. ASP.NET MVC 快速开发框架之 SqlSugar+SyntacticSugar+JQWidgetsSugar+jqwidgets(转)

    jqwidgets.js: 是一个功能完整的框架,它具有专业的可触摸的jQuery插件.主题.输入验证.拖放插件.数据适配器,内置WAI-ARIA(无障碍网页应用)可访问性.国际化和MVVM模式支持. ...

  8. ASP.NET MVC 快速开发框架之 SqlSugar+SyntacticSugar+JQWidgetsSugar+jqwidgets

    jqwidgets.js: 是一个功能完整的框架,它具有专业的可触摸的jQuery插件.主题.输入验证.拖放插件.数据适配器,内置WAI-ARIA(无障碍网页应用)可访问性.国际化和MVVM模式支持. ...

  9. MVC 快速开发框架

    ASP.NET MVC 快速开发框架之 SqlSugar+SyntacticSugar+JQWidgetsSugar+jqwidgets jqwidgets.js: 是一个功能完整的框架,它具有专业的 ...

随机推荐

  1. Spring Boot (15) pom.xml设置

    继承spring-boot-parent 要成为一个spring boot项目,首先就必须在pom.xml中继承spring-boot-starter-parent,同时制定其版本 <paren ...

  2. 在PL/SQL中使用游标、动态sql和绑定变量的小例子

    需求:查询并输出30号部门的雇员信息 方式一:使用 loop...fetch SET serveroutput ON; DECLARE CURSOR c_emp IS ; v_emp emp%rowt ...

  3. OPPO R9sPlus MIFlash线刷TWRP Recovery ROOT详细教程

    教程转载来自 残芯此生不换  OPPO R9sPlus 目前最简单的刷Recovery root 方法,强烈推荐 新机想要刷第三方卡刷包的最简单过程是:           手机关机-->下载M ...

  4. python2打印list中文内容防乱码

    zh_ls = ['人','民'] print str(zh_ls).decode("string_escape")

  5. QT4使用HDF5 类型错误

    使用HDF5 :HDF5_1.10.0 出现: fatal error C1083: 无法打开包括文件:"stdbool.h": No such file or directory ...

  6. c#日期计算

    /// <summary> /// 计算日期的间隔(静态类) /// </summary> public static class dateTimeDiff { /// < ...

  7. Arduino控制DTH11模块

    一.接线原理图 二.实物图 三.事例代码 下载 git clone https://github.com/adafruit/DHT-sensor-library.git 放到 arduino-1.6. ...

  8. 团体程序设计天梯赛-练习集-L1-035. 情人节

    L1-035. 情人节 以上是朋友圈中一奇葩贴:“2月14情人节了,我决定造福大家.第2个赞和第14个赞的,我介绍你俩认识…………咱三吃饭…你俩请…”.现给出此贴下点赞的朋友名单,请你找出那两位要请客 ...

  9. AD 域服务简介(一)- 基于 LDAP 的 AD 域服务器搭建及其使用(转)

    一.前言 1.1 AD 域服务 什么是目录(directory)呢? 日常生活中使用的电话薄内记录着亲朋好友的姓名.电话与地址等数据,它就是 telephone directory(电话目录):计算机 ...

  10. eas之编辑表单元格

    --指定表列行单元不可编辑 // 锁定表格.行.列.单元 table.getStyleAttributes().getProtection().setLocked(true); row.getStyl ...