通用扩展函数之TypeParse
代码实现:
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的更多相关文章
- ASP.NETC#通用扩展函数之TypeParse 类型转换方便多了
用法: var int1 = "2".TryToInt();//转换为int失败返回0 var int2 = "2x".TryToInt(); var int3 ...
- C#ASP.NET 通用扩展函数之 LogicSugar 简单好用
说明一下性能方面 还可以接受 循环1000次普通Switch是用了0.001秒 ,扩展函数为0.002秒 , 如果是大项目在有负载均衡的情况下完全可以无视掉,小项目也不会计较这点性能了. 注意需要引 ...
- C#ASP.NET 通用扩展函数之 IsWhat 简单好用
好东西都需要人去整理.分类 注意:需要引用命名空间 SyntacticSugar 用法: /***扩展函数名细***/ //[IsInRange] int num = 100; //以前写法 if ( ...
- 功能齐全并且比较时髦的Jquery通用开源框架之【ejq.js】
简介 ejq是一款非常小巧的JS工具库,未压缩才50K,在jquery的基础上对jquery缺失部分作了很好的弥补作用. 优点: 1.具有内置的模板解析引擎语法和angularjs相近减少学习成本 2 ...
- [转]Jquery通用开源框架之【ejq.js】
ejq是一款非常小巧的JS工具库,未压缩才50K,在jquery的基础上对jquery缺失部分作了很好的弥补作用. 优点: 1.具有内置的模板解析引擎语法和angularjs相近减少学习成本 2.能够 ...
- query通用开源框架
Jquery通用开源框架之[ejq.js] 简介 ejq是一款非常小巧的JS工具库,未压缩才50K,在jquery的基础上对jquery缺失部分作了很好的弥补作用. 优点: 1.具有内置的模板解析引擎 ...
- ASP.NET MVC 快速开发框架之 SqlSugar+SyntacticSugar+JQWidgetsSugar+jqwidgets(转)
jqwidgets.js: 是一个功能完整的框架,它具有专业的可触摸的jQuery插件.主题.输入验证.拖放插件.数据适配器,内置WAI-ARIA(无障碍网页应用)可访问性.国际化和MVVM模式支持. ...
- ASP.NET MVC 快速开发框架之 SqlSugar+SyntacticSugar+JQWidgetsSugar+jqwidgets
jqwidgets.js: 是一个功能完整的框架,它具有专业的可触摸的jQuery插件.主题.输入验证.拖放插件.数据适配器,内置WAI-ARIA(无障碍网页应用)可访问性.国际化和MVVM模式支持. ...
- MVC 快速开发框架
ASP.NET MVC 快速开发框架之 SqlSugar+SyntacticSugar+JQWidgetsSugar+jqwidgets jqwidgets.js: 是一个功能完整的框架,它具有专业的 ...
随机推荐
- 【PostgreSQL-9.6.3】log参数的设置
编辑数据目录中的postgresql.conf参数文件,我的数据目录是/usr/local/pgsql/data vi postgresql.conf 找到如下内容: ... #----------- ...
- VHDL之User-defined data types
VHDL allows the user to define own data types. 1 user-defined integer types -- This is indeed the pr ...
- error C3859: 超过了PCH的虚拟内存范围;请使用“-Zm33”或更大的命令行选项重新编译
编译 ORB_SLAM的Release版本时,出现了此问题: 错误 2 error C3859: 超过了 PCH 的虚拟内存范围;请使用"-Zm465"或更大的命令行 修改方法: ...
- Spring 团队开源 nohttp,尽可能不用 HTTP
Spring 团队开源 nohttp 项目,用以查找.替换和阻止 http:// 的使用. 项目是为了在可能使用 https:// 的情况下不使用到 http://,确保不会发生中间人攻击.Sprin ...
- [接口管理平台] eoLinker AMS 专业版 V3.5 :加入数据结构管理、通用函数管理、API 快速测试等近 30 项更新
eoLinker AMS是集API文档管理.API自动化测试.开发协作三位一体的综合API开发管理平台,是中国最大的在线API管理平台.目前eoLinker AMS已经为来自全球的超过两万家企业托管超 ...
- vue 导航菜单默认子路由
export default new Router({ routes: [ { path: '/', name: 'index', component: index, children: [ { pa ...
- eas之获取单据编码规则
//获取单据编码规则 /*** @Title: getNumber* @Description: TODO(获取单据编码规则)* <p>* @date 201 ...
- [Ynoi2015]盼君勿忘
题目大意: 给定一个序列,每次查询一个区间\([l,r]\)中所有子序列分别去重后的和\(\bmod p\)(每次询问模数不同). 解题思路: 在太阳西斜的这个世界里,置身天上之森.等这场战争结束之后 ...
- 爬虫系列(十) 用requests和xpath爬取豆瓣电影
这篇文章我们将使用 requests 和 xpath 爬取豆瓣电影 Top250,下面先贴上最终的效果图: 1.网页分析 (1)分析 URL 规律 我们首先使用 Chrome 浏览器打开 豆瓣电影 T ...
- Hexo系列(二) 配置文件详解
Hexo 是一款优秀的博客框架,在使用 Hexo 搭建一个属于自己的博客网站后,我们还需要对其进行配置,使得 Hexo 更能满足自己的需求 这里所说的配置文件,是位于站点根目录下的 _config.y ...