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 ...
随机推荐
- netsh-winsock-reset;ping的通公网IP和DNS地址和内网网关,就是不能解析域名;
winXP cmd-------------> netsh winsock reset ============= 相关知识: netsh winsock reset命令含义是重置 Winsoc ...
- finished with non-zero exit 添加v7包报错的问题
错误: 添加 compile 'com.android.support:appcompat-v7:22.2.0'后报错,里面有其它的jar包,但是只要添加这个v7包就报错. Error:Executi ...
- 网页内容导出word/excel的js代码
IE设置: 工具-> Internet选项-> 安全->自定义级别-> 对没有标记安全级别的ActiveX控件进行初始化 设为启用! 1.导出word //指定区域导出到Wo ...
- The Truth About .NET Objects And Sharing Them Between AppDomains
From http://geekswithblogs.net/akraus1/archive/2012/07/25/150301.aspx I have written already some ti ...
- Asp.net Core WebApi 全局异常类
通过全局异常类,所有程序中遇到的错误都会被拦截,并友好的返回结果. 1.自定义一个全局异常处理类中间件 using Microsoft.AspNetCore.Http; using Newtonsof ...
- ch2 创建和销毁对象
ch2 创建和销毁对象
- C#集合 -- Lists,Queues, Stacks 和 Sets
List<T>和ArrayList Generic的List和非Generic的ArrayList类支持可变化大小的对象数组,它们也是最常见的集合类.ArrayList实现了IList接口 ...
- Flink 案例整合
1.概述 Flink 1.1.0 版本已经在官方发布了,官方博客于 2016-08-08 更新了 Flink 1.1.0 的变动.在这 Flink 版本的发布,添加了 SQL 语法这一特性.这对于业务 ...
- 点击弹出 +1放大效果 -- jQuery插件
20140110更新: <!doctype html> <html> <head> <meta charset="UTF-8"> & ...
- Python strange questions list
sys.setrecursionlimit(1<<64) Line 3: OverflowError: Python int too large to convert to C long ...