Usage:
var int1 = "2". Trytoint ();//Convert to int failed to return 0 var int2 = "2x". Trytoint (); var int3 = "2". Trytoint (1);//Convert to int failed to return 1 var int4 = "2x". Trytoint (1); var D1 = "2". Trytomoney (); Ibid. 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 and model convert var JSON = new {id = 1}. Modeltojson (); var model = "{id:1}". Jsontomodel<modeltest> (); List and DataTable convert var dt = new list<modeltest> (). ListtodatatablE (); var list = dt. Datatabletolist<modeltest> ();
Code:
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>//* Description: Type conversion//* * Founding Date: 2015-6-2//* * Modified:-//* * Sunkaixuan * * Instructions for use://</summary> public static class Typeparseextenions {#region strong to int if failure returns 0 <summary>//strong turn int if failure returns 0//</summary>//<param name= "Thisvalue" ></p aram>//<param name= "I" ></param>///<returns></returns> public static I NT Trytoint (This object thisvalue) {int reval = 0; if (thisvalue! = null && int. TryParse (Thisvalue.tostring (), out Reval)) {return reval; } return Reval; } #endregion #region Strong to int if failure returns errorvalue<summary>//strong turn int if failed return errorvalue///</summary>//<param name= "Thisvalue" & gt;</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 Strong to double if failure returns 0//<summary>//strong turn into Money if failure returns 0///& lt;/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 Strong to double if failure returns ERRORVALUE//<summary>//strong to double if failure returns Erro RValue//</summary>//<param name= "Thisvalue" ></param>//<param name= "Erro RValue "></param>///<returns></returns> public static double Trytomoney (This object th Isvalue, int errorvalue) {double Reval = 0; if (thisvalue! = null && double. TryParse (Thisvalue.tostring (), out Reval)) {return reval; } return errorvalue; #endregion #region strongly turns into a string if the failure returns ""///<summary>///strong to string if the failure returns ""// /</summary>//<param name= "Thisvalue" ></param>//<param name= "I" ></param> <returns></returns> Public static string trytostring (This object thisvalue) {if (thisvalue! = null) return thisvalue.tostring (). T Rim (); Return ""; #endregion #region strongly turns into a string if the failure returns ERRORVALUE//<summary>///strong to string if the failure returns STR </summary>//<param name= "Thisvalue" ></param>//<param name= "Errorvalue "></param>///<returns></returns> public static string trytostring (This object thisval UE, String errorvalue) {if (thisvalue! = null) return thisvalue.tostring (). Trim (); return errorvalue; } #endregion #region Strong to decimal if failure returns 0//<summary>//strong to Decimal if failure returns 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 Strong to decimal if failure returns ERRORVALUE//<summary>//strong to Decimal if failure returns ER Rorvalue//</summary>//<param name= "Thisvalue" ></param>//<param name= "er Rorvalue "></param>///<returns></returns> public static Decimal Trytodecimal (This obje CT thisvalue, int errorvalue) {Decimal Reval = 0; if (thisvalue! = null && decimal. TryParse (Thisvalue.tostring (), out Reval)) {return reval; } return errorvalue; } #endregion #region Strong turn to DateTime if failure returns DATETIME.MINVALUE//<summary>///Strong to DateTime If the failure returns 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 re Val } return Reval; } #endregion #region Strong turn to datetime if the failure returns ERRORVALUE//<summary>///strong to DateTime if the failure returns Errorvalue//</summary>//<param name= "Thisvalue" ></param>//<param name= " Errorvalue "></param>///<returns></returns> public static DateTime trytodate (This obje CT thisvalue, datetime errorvalue) {datetime reval = Datetime.minvalue; if (thisvalue! = null && datetime.tryparse (thIsvalue.tostring (), out Reval) {return reval; } return errorvalue; } #endregion #region JSON conversion///<summary>//JSON serialized as entity///</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>//To serialize entities to 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>//Convert collection class to DataTable/ </summary>//<param name= "List" > Collection </param>//<returns></returns> public static DataTable listtodatatable<t> (this list<t> List) {DataTable result = new Data Table (); 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>//Convert DataTable to List///</summary>//<typeparam name= "T" > </typeparam>//<param name= "DT" ></param>//<returns></returns> Publ IC 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}}
The Typeparse type conversion of asp.netc# universal extension function is much more convenient.