C # Tool Classes--generic methods for type conversions

Source: Internet
Author: User
C # Tool Classes--generic methods for type conversions

Using system;using system.collections.generic;using system.linq;using system.text;using System.Data;namespace Linqpractice{class Utility {public static T convertdatarow<t> (DataRow dr, string columnname,t default Value) {if (Dr.            Table.Columns.Contains (ColumnName)) {return converttype<t> (dr[columnname],defaultvalue);        } return default (T); }///<summary>///generic data type conversions///</summary>//<typeparam name= "T" > Custom data Types & lt;/typeparam>//<param name= "value" > Incoming value required for conversion </param>//<param name= "DefaultValue" > Default value </param>///<returns></returns> public static T converttype<t> (Object Value,t de            Faultvalue) {try {return (T) converttot<t> (Value,defaultvalue);     } catch {return default (T);       }}///<summary>//Conversion data type///</summary>//<typeparam name= " T "> Custom data Type </typeparam>//<param name=" myvalue "> Incoming value to convert </param>///<param name=" D Efaultvalue "> Default value </param>//<returns></returns> private static Object converttot<t&            gt; (Object myvalue,t defaultvalue) {TypeCode TypeCode = System.Type.GetTypeCode (typeof (T));                if (myvalue! = null) {String value = Convert.ToString (myvalue);                        Switch (typecode) {case TypeCode.Boolean:bool flag = false; if (bool.                        TryParse (value, out flag)) {return flag;                    } break;                        Case TypeCode.Char:char C; if (Char.tryparSE (value, out C)) {return C;                    } break;                        Case TypeCode.SByte:sbyte s = 0;                        if (Sbyte.tryparse (value, out s)) {return s;                    } break;                        Case TypeCode.Byte:byte B = 0;                        if (Byte.tryparse (value, out B)) {return B;                    } break;                        Case TypeCode.Int16:Int16 i16 = 0;                        if (Int16.tryparse (value, out i16)) {return i16;                    } break;                        Case TypeCode.UInt16:UInt16 ui16 = 0; if (UInt16.TryParse (value, out ui16)) return ui16;                    Break                        Case TypeCode.Int32:int i = 0;                        if (Int32.TryParse (value, out i)) {return i;                    } break;                        Case TypeCode.UInt32:UInt32 ui32 = 0;                        if (Uint32.tryparse (value, out ui32)) {return ui32;                    } break;                        Case TypeCode.Int64:Int64 i64 = 0;                        if (Int64.tryparse (value, out i64)) {return i64;                    } break;                        Case TypeCode.UInt64:UInt64 ui64 = 0; if (Uint64.tryparse (value, out ui64)) return ui64;                    Break                        Case TypeCode.Single:Single single = 0;                        if (Single.tryparse (value, out of single)) {return single;                    } break;                        Case TypeCode.Double:double d = 0;                        if (Double.tryparse (value, out D)) {return D;                    } break;                        Case TypeCode.Decimal:decimal de = 0;                        if (Decimal.tryparse (value, out de)) {return de;                    } break;                        Case TypeCode.DateTime:DateTime DT;   if (Datetime.tryparse (value, out DT))                     {return dt;                    } break; Case TypeCode.String:if (!string. IsNullOrEmpty (value)) {return value.                        ToString ();                } break;        }} return defaultvalue; }    }}

The above is the content of the generic method of the C # tool class-type conversion, please pay attention to topic.alibabacloud.com (www.php.cn) for more relevant content.

  • 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.