ASP. NETC # It is much easier to convert the TypeParse type of general extension functions,

Source: Internet
Author: User

ASP. NETC # It is much easier to convert the TypeParse type of general extension functions,

 

 

Usage:

Var int1 = "2 ". tryToInt (); // If int conversion fails, 0 var int2 = "2x" is returned ". tryToInt (); var int3 = "2 ". tryToInt (1); // If int conversion fails, 1 var int4 = "2x" is returned ". tryToInt (1); var d1 = "2 ". tryToMoney (); // same as var d2 = "2x ". tryToMoney (); var d3 = "2 ". tryToMoney (1); var d4 = "2x ". tryToMoney (1); string a = null; var s1 =. tryToString (); var s3 =. 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 1-1 ". tryToDate (); var de2 = "x2013-1-1 ". tryToDate (); var de3 = "x2013-1-1 ". tryToDate (DateTime. now); // convert var json = new {id = 1} from json and model }. modelToJson (); var model = "{id: 1 }". jsonToModel <ModelTest> (); // convert list and dataTable to 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 // ** Start Time: // ** modification time: -/// ** Author: sunkaixuan // ** usage instructions: /// </summary> public static class TypeParseExtenions {# region is strongly converted to int. If it fails, 0 is returned. // <summa Ry> /// convert to int. If the conversion fails, 0 is returned. /// </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 ;} # endregion # region is strongly converted to int. If an error occurs, errorValue is returned. // <summary> // It is strongly converted to int. If a failure occurs, errorValue is returned. // </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 # convert region to double. If the result fails, 0 is returned. // <summary> // convert the value to money. If the result fails, 0 is returned. /// </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 is strongly converted to double. If an error occurs, errorValue is returned. // <summary> // if a failure occurs, errorValue is returned. // </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 # Strong conversion of region to string returns "" // <summary> // if it fails to be converted to string, return "" // </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 is strongly converted to string. If the result fails, errorValue is returned. // <summary> // It is strongly converted to string. If the result fails, str is returned. /// </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 is strongly converted to Decimal. If a failure occurs, 0 is returned. // <summary> // if a failure occurs, 0 is returned. // </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 # Strong conversion of region to Decimal returns errorValue if it fails // <summary> // strong conversion to Decimal returns errorValue if it fails /// </summary> /// <param name = "thisValue"> </param> // <param name = "errorValue"> </param> // <returns> </returns> public static Decimal TryToDecimal (this object thisValue, int errorValue) {Decimal reval = 0; if (th IsValue! = Null & decimal. tryParse (thisValue. toString (), out reval) {return reval;} return errorValue ;}# endregion # convert strongly to DateTime if the return value fails. minValue /// <summary> /// convert to DateTime. If the conversion fails, return DateTime. minValue // </summary> /// <param name = "thisValue"> </param> /// <param name = "I"> </param> // /<returns> </returns> public static DateTime TryToDate (this object thisValue) {DateTime reval = DateTime. M InValue; if (thisValue! = Null & DateTime. tryParse (thisValue. toString (), out reval) {return reval ;} # endregion # region is strongly converted to DateTime. If a failure occurs, errorValue is returned. // <summary> // It is strongly converted to DateTime. If a failure occurs, errorValue is returned. // </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 conversion // <summary> // serialize json to an object // </summary> /// <typeparam name = "TEntity"> </ typeparam> // <param name = "json"> </param> // <returns> </returns> public static TEntity JsonToModel <TEntity> (this string json) {JavaScriptSerializer jsSerializer = new JavaScriptSerializer (); ret Urn jsSerializer. deserialize <TEntity> (json );} /// <summary> /// serialize the object 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 the collection class to a DataTable /// </summary> /// <param Name = "list"> set </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 (Pr OpertyInfo 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 the able to 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 <Pro PertyInfo> (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 }}

  

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.