C#、ASP.NET汎用拡張ツール類のType Parse
10097 ワード
使用方法:
コード:
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();
//list dataTable
var dt = new List().ListToDataTable();
var list = dt.DataTableToList();
コード:
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
{
///
/// ** :
/// ** :2015-6-2
/// ** :-
/// ** :sunkaixuan
/// ** :
///
public static class TypeParseExtenions
{
#region int 0
///
/// int 0
///
///
///
///
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
///
/// int errorValue
///
///
///
///
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
///
/// money 0
///
///
///
///
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
///
/// double errorValue
///
///
///
///
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 ""
///
/// string ""
///
///
///
///
public static string TryToString(this object thisValue)
{
if (thisValue != null) return thisValue.ToString().Trim();
return "";
}
#endregion
#region string errorValue
///
/// string str
///
///
///
///
public static string TryToString(this object thisValue, string errorValue)
{
if (thisValue != null) return thisValue.ToString().Trim();
return errorValue;
}
#endregion
#region Decimal 0
///
/// Decimal 0
///
///
///
///
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
///
/// Decimal errorValue
///
///
///
///
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
///
/// DateTime DateTime.MinValue
///
///
///
///
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
///
/// DateTime errorValue
///
///
///
///
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
///
/// json
///
///
///
///
public static TEntity JsonToModel(this string json)
{
JavaScriptSerializer jsSerializer = new JavaScriptSerializer();
return jsSerializer.Deserialize(json);
}
///
/// json
///
///
///
public static string ModelToJson(this T model)
{
JavaScriptSerializer jsSerializer = new JavaScriptSerializer();
return jsSerializer.Serialize(model);
}
#endregion
#region DataTable List
///
/// DataTable
///
///
///
public static DataTable ListToDataTable(this List 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;
}
///
/// datatable list
///
///
///
///
public static List DataTableToList(this DataTable dt)
{
var list = new List();
Type t = typeof(T);
var plist = new List(typeof(T).GetProperties());
foreach (DataRow item in dt.Rows)
{
T s = System.Activator.CreateInstance();
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
}
}