ASP.NETC#通用扩展函数之TypeParse 类型转换方便多了
用法:
var int1 = "2".TryToInt();//转换为int失败返回0
var int2 = "2x".TryToInt();
var int3 = "2".TryToInt(1);//转换为int失败返回1
var int4 = "2x".TryToInt(1); var d1 = "2".TryToMoney(); //同上
var d2 = "2x".TryToMoney();
var d3 = "2".TryToMoney(1);
var d4 = "2x".TryToMoney(1); string a = null;
var s1 = a.TryToString();
var s3 = a.TryToString("1"); var d11 = "2".TryToDecimal();
var d22 = "2x".TryToDecimal();
var d33 = "2".TryToDecimal(1);
var d44 = "2x".TryToDecimal(1); 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 = 1 }.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 = 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 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 > 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);
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 = 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 }
}
ASP.NETC#通用扩展函数之TypeParse 类型转换方便多了的更多相关文章
- C#ASP.NET 通用扩展函数之 LogicSugar 简单好用
说明一下性能方面 还可以接受 循环1000次普通Switch是用了0.001秒 ,扩展函数为0.002秒 , 如果是大项目在有负载均衡的情况下完全可以无视掉,小项目也不会计较这点性能了. 注意需要引 ...
- C#ASP.NET 通用扩展函数之 IsWhat 简单好用
好东西都需要人去整理.分类 注意:需要引用命名空间 SyntacticSugar 用法: /***扩展函数名细***/ //[IsInRange] int num = 100; //以前写法 if ( ...
- 通用扩展函数之TypeParse
代码实现: ".TryToInt();//转换为int失败返回0 var int2 = "2x".TryToInt(); );//转换为int失败返回1 ); " ...
- ASP.NET通用权限组件思路设计
开篇 做任何系统都离不开和绕不过权限的控制,尤其是B/S系统工作原理的特殊性使得权限控制起来更为繁琐,所以就在想是否可以利用IIS的工作原理,在IIS处理客户端请求的某个入口或出口通过判断URL来达到 ...
- ASP.NET通用权限系统快速开发框架
系统在线演示地址: http://120.90.2.126:8051 登录账户:system,密码:system### DEMO下载地址: http://download.csdn.net/detai ...
- asp.net 通用的连接数据库实例代码
asp.net中数据库连接代码,有需要的朋友可以参考一下. <%@ Page Language="C#" AutoEventWireup="true" C ...
- Asp.Net Unix时间戳和DateTime类型转换
using System; using System.Collections.Generic; using System.Web; using System.Web.UI; using System. ...
- asp网站通用后台代码设计
main2.css: a:link {color: #333333; text-decoration: none}a:visited {color: #000000; text-decoration: ...
- ASP.NET通用权限验证组件实现
沙发(SF)通用权限验证组件 开篇 本篇介绍通用权限验证的实现代码思路,总共分为导入参数.解析XML.根据XML配置进行处理.返回结果. 代码架构图 1. 类介绍 1.SFWebPermissio ...
随机推荐
- Access to the path '20141211142713.gif' is denied.
给network service加上读写权限即可
- Codeforces Round #383 (Div. 2) 题解【ABCDE】
Codeforces Round #383 (Div. 2) A. Arpa's hard exam and Mehrdad's naive cheat 题意 求1378^n mod 10 题解 直接 ...
- python数据结构之栈、队列的实现
这个在官网中list支持,有实现. 补充一下栈,队列的特性: 1.栈(stacks)是一种只能通过访问其一端来实现数据存储与检索的线性数据结构,具有后进先出(last in first out,LIF ...
- JAVA中类、实例与Class对象
已同步更新至个人blog:http://dxjia.cn/2015/08/java-class-object/ 类 类是面向对象编程语言的一个重要概念,它是对一项事物的抽象概括,可以包含该事物的一些属 ...
- 学习OpenGL简单易懂网站
帅炸天的教程:欢迎来到OpenGL的世界
- No connection string named '***' could be found in the application config file
Code-First时更新数据库遇到妖孽问题“No connection string named '***' could be found in the application config fil ...
- ASP.NET 4.0 forms authentication issues with IE11
As I mentioned earlier, solutions that rely on User-Agent sniffing may break, when a new browser or ...
- 实例详解 DB2 排序监控和调优
实例详解 DB2 排序监控和调优http://automationqa.com/forum.php?mod=viewthread&tid=2882&fromuid=2
- Hadoop 2.4.1 Map/Reduce小结【原创】
看了下MapReduce的例子.再看了下Mapper和Reducer源码,理清了参数的意义,就o了. public class Mapper<KEYIN, VALUEIN, KEYOUT, VA ...
- Swift 集合类型
Swift语言提供数组和字典的集合类型 Swift 语言里的数组和字典中存储的数据值类型必须明确 ,即数组中只能存放同类型的数据. 1: 数组 数组的创建 var shoppingList: St ...