context: /**********************************************************\
  |                                                          |
  | The implementation of PHPRPC Protocol 3.0                |
  |                                                          |
  | PHPConvert.cs                                            |
  |                                                          |
  | Release 3.0.2                                            |
  | Copyright by Team-PHPRPC                                 |
  |                                                          |
  | WebSite:  http://www.phprpc.org/                         |
  |           http://www.phprpc.net/                         |
  |           http://www.phprpc.com/                         |
  |           http://sourceforge.net/projects/php-rpc/       |
  |                                                          |
  | Authors:  Ma Bingyao <andot@ujn.edu.cn>                  |
  |                                                          |
  | This file may be distributed and/or modified under the   |
  | terms of the GNU General Public License (GPL) version    |
  | 2.0 as published by the Free Software Foundation and     |
  | appearing in the included file LICENSE.                  |
  |                                                          |
  \**********************************************************/
  
  /* PHP Convert library.
   *
   * Copyright: Ma Bingyao <andot@ujn.edu.cn>
   * Version: 3.0
   * LastModified: Apr 12, 2010
   * This library is free.  You can redistribute it and/or modify it under GPL.
   */
  
  namespace org.phprpc.util {
      using System;
      using System.Text;
      using System.Globalization;
      using System.Collections;
  #if !(PocketPC || Smartphone || WindowsCE || NET1)
      using System.Collections.Generic;
  #endif
      using System.Reflection;
  
      public sealed class PHPConvert {
          private static readonly IFormatProvider provider;
          private static readonly Encoding UTF8;
          private static readonly Type typeofArrayList;
          private static readonly Type typeofAssocArray;
          private static readonly Type typeofBoolean;
          private static readonly Type typeofBigInteger;
          private static readonly Type typeofByte;
          private static readonly Type typeofByteArray;
          private static readonly Type typeofChar;
          private static readonly Type typeofCharArray;
          private static readonly Type typeofDateTime;
          private static readonly Type typeofDBNull;
          private static readonly Type typeofDecimal;
          private static readonly Type typeofDouble;
          private static readonly Type typeofHashtable;
          private static readonly Type typeofICollection;
          private static readonly Type typeofIDictionary;
          private static readonly Type typeofIList;
          private static readonly Type typeofInt16;
          private static readonly Type typeofInt32;
          private static readonly Type typeofInt64;
          private static readonly Type typeofObject;
          private static readonly Type typeofObjectArray;
          private static readonly Type typeofSByte;
          private static readonly Type typeofSingle;
          private static readonly Type typeofString;
          private static readonly Type typeofStringBuilder;
          private static readonly Type typeofUInt16;
          private static readonly Type typeofUInt32;
          private static readonly Type typeofUInt64;
  #if (Mono)
          private static readonly Type typeofMonoBigInteger;
  #endif
  #if !(PocketPC || Smartphone || WindowsCE || NET1)
          private static readonly Type typeofGDictionary;
          private static readonly Type typeofGList;
  #endif
          private static readonly Char[] m_trimmableChars;
  
          private PHPConvert() {
          }
  
          static PHPConvert() {
              provider = CultureInfo.InvariantCulture;
              UTF8 = new UTF8Encoding();
              typeofDBNull = typeof(System.DBNull);
              typeofString = typeof(String);
              typeofStringBuilder = typeof(StringBuilder);
              typeofBoolean = typeof(Boolean);
              typeofBigInteger = typeof(BigInteger);
              typeofByte = typeof(Byte);
              typeofByteArray = typeof(Byte[]);
              typeofChar = typeof(Char);
              typeofCharArray = typeof(Char[]);
              typeofDecimal = typeof(Decimal);
              typeofDouble = typeof(Double);
              typeofInt16 = typeof(Int16);
              typeofInt32 = typeof(Int32);
              typeofInt64 = typeof(Int32);
              typeofSByte = typeof(SByte);
              typeofSingle = typeof(Single);
              typeofDateTime = typeof(DateTime);
              typeofUInt16 = typeof(UInt16);
              typeofUInt32 = typeof(UInt32);
              typeofUInt64 = typeof(UInt64);
              typeofObject = typeof(Object);
              typeofObjectArray = typeof(Object[]);
              typeofArrayList = typeof(ArrayList);
              typeofHashtable = typeof(Hashtable);
              typeofAssocArray = typeof(AssocArray);
              typeofICollection = typeof(ICollection);
              typeofIDictionary = typeof(IDictionary);
              typeofIList = typeof(IList);
  #if (Mono)
              typeofMonoBigInteger = typeof(Mono.Math.BigInteger);
  #endif
  #if !(PocketPC || Smartphone || WindowsCE || NET1)
              typeofGDictionary = typeof(Dictionary<,>);
              typeofGList = typeof(List<>);
  #endif
              m_trimmableChars = new Char[] {(char)0x0, (char) 0x9, (char) 0xA, (char) 0xB, (char) 0xC, (char) 0xD,
  			(char) 0x85, (char) 0x1680, (char) 0x2028, (char) 0x2029,
  			(char) 0x20, (char) 0xA0, (char) 0x2000, (char) 0x2001, (char) 0x2002, (char) 0x2003, (char) 0x2004,
  			(char) 0x2005, (char) 0x2006, (char) 0x2007, (char) 0x2008, (char) 0x2009, (char) 0x200A, (char) 0x200B,
  			(char) 0x3000, (char) 0xFEFF };
          }
  
          #region ChangeType
  
          public static Object ChangeType(Object value, Type conversionType) {
              return ChangeType(value, conversionType, UTF8);
          }
  
          public static Object ChangeType(Object value, Type conversionType, String charset) {
              return ChangeType(value, conversionType, Encoding.GetEncoding(charset));
          }
  
          public static Object ChangeType(Object value, Type conversionType, Encoding encoding) {
              if (conversionType == null) {
                  throw new ArgumentNullException("conversionType");
              }
              if (conversionType.IsByRef) {
                  conversionType = conversionType.GetElementType();
              }
              if (value == null) {
                  if (conversionType.IsValueType) {
                      throw new InvalidCastException("Null object cannot be converted to a value type.");
                  }
                  return null;
              }
              if ((conversionType == typeofObject) || conversionType.IsInstanceOfType(value)) {
                  return value;
              }
              if (conversionType.IsEnum) {
                  return ToEnum(value, conversionType);
              }
              if (conversionType == typeofBoolean) {
                  return ToBoolean(value);
              }
              if (conversionType == typeofBigInteger) {
                  return ToBigInteger(value);
              }
              if (conversionType == typeofChar) {
                  return ToChar(value);
              }
              if (conversionType == typeofSByte) {
                  return ToSByte(value);
              }
              if (conversionType == typeofByte) {
                  return ToByte(value);
              }
              if (conversionType == typeofInt16) {
                  return ToInt16(value);
              }
              if (conversionType == typeofUInt16) {
                  return ToUInt16(value);
              }
              if (conversionType == typeofInt32) {
                  return ToInt32(value);
              }
              if (conversionType == typeofUInt32) {
                  return ToUInt32(value);
              }
              if (conversionType == typeofInt64) {
                  return ToInt64(value);
              }
              if (conversionType == typeofUInt64) {
                  return ToUInt64(value);
              }
              if (conversionType == typeofSingle) {
                  return ToSingle(value);
              }
              if (conversionType == typeofDouble) {
                  return ToDouble(value);
              }
              if (conversionType == typeofDecimal) {
                  return ToDecimal(value);
              }
              if (conversionType == typeofDateTime) {
                  return ToDateTime(value);
              }
              if (conversionType == typeofString) {
                  return ToString(value, encoding);
              }
              if (conversionType == typeofStringBuilder) {
                  return ToStringBuilder(value, encoding);
              }
              if (conversionType == typeofByteArray) {
                  return ToByteArray(value, encoding);
              }
              if (conversionType == typeofCharArray) {
                  return ToCharArray(value, encoding);
              }
              if (conversionType == typeofDBNull) {
                  return DBNull.Value;
              }
              if (conversionType == typeofArrayList) {
                  return ToArrayList(value);
              }
              if (conversionType == typeofHashtable) {
                  return ToHashtable(value);
              }
              if (conversionType == typeofAssocArray) {
                  return ToAssocArray(value);
              }
              if (conversionType.IsArray) {
                  return ToArray(value, conversionType, encoding);
              }
  #if (Mono)
              if (conversionType == typeofMonoBigInteger) {
                  return ToMonoBigInteger(value);
              }
  #endif
  #if !(PocketPC || Smartphone || WindowsCE || NET1)
              if (conversionType.IsGenericType) {
                  Type gtd = conversionType.GetGenericTypeDefinition();
                  if (gtd == typeofGDictionary) {
                      return ToGDictionary(value, conversionType, encoding);
                  }
                  if (gtd == typeofGList) {
                      return ToGList(value, conversionType, encoding);
                  }
              }
  #endif
              if (typeofIDictionary.IsAssignableFrom(conversionType)) {
                  return ToIDictionary(value, conversionType);
              }
              if (typeofIList.IsAssignableFrom(conversionType)) {
                  return ToIList(value, conversionType);
              }
              if (typeofICollection.IsAssignableFrom(conversionType)) {
                  return ToICollection(value, conversionType);
              }
              return ToObject(value, conversionType, encoding);
          }
  
          public static Object ChangeType(Object value, TypeCode typeCode) {
              return ChangeType(value, typeCode, UTF8);
          }
  
          public static Object ChangeType(Object value, TypeCode typeCode, String charset) {
              return ChangeType(value, typeCode, Encoding.GetEncoding(charset));
          }
  
          public static Object ChangeType(Object value, TypeCode typeCode, Encoding encoding) {
              switch (typeCode) {
              case TypeCode.Empty:
                  return null;
  
              case TypeCode.Object:
                  return value;
  
              case TypeCode.DBNull:
                  return DBNull.Value;
  
              case TypeCode.Boolean:
                  return ToBoolean(value);
  
              case TypeCode.Char:
                  return ToChar(value);
  
              case TypeCode.SByte:
                  return ToSByte(value);
  
              case TypeCode.Byte:
                  return ToByte(value);
  
              case TypeCode.Int16:
                  return ToInt16(value);
  
              case TypeCode.UInt16:
                  return ToUInt16(value);
  
              case TypeCode.Int32:
                  return ToInt32(value);
  
              case TypeCode.UInt32:
                  return ToUInt32(value);
  
              case TypeCode.Int64:
                  return ToInt64(value);
  
              case TypeCode.UInt64:
                  return ToUInt64(value);
  
              case TypeCode.Single:
                  return ToSingle(value);
  
              case TypeCode.Double:
                  return ToDouble(value);
  
              case TypeCode.Decimal:
                  return ToDecimal(value);
  
              case TypeCode.DateTime:
                  return ToDateTime(value);
  
              case TypeCode.String:
                  return ToString(value, encoding);
              }
              throw new ArgumentException("Unknown TypeCode value.");
          }
  
          #endregion
  
          #region ToBoolean
  
          public static Boolean ToBoolean(Boolean value) {
              return value;
          }
  
          public static Boolean ToBoolean(Byte value) {
              return (value != 0);
          }
  
          public static Boolean ToBoolean(Byte[] value) {
              if ((value == null) || (value.Length == 0) ||
                  (value.Length == 1 && (value[0] == 0 || value[0] == 0x30))) {
                  return false;
              }
              String s = ToString(value);
              if ("false".Equals(s) ||
                  "false".Equals(s.Trim(m_trimmableChars).ToLower(CultureInfo.InvariantCulture))) {
                  return false;
              }
              return true;
          }
  
          public static Boolean ToBoolean(Char value) {
              return (value != '\0') && (value != '0');
          }
  
          public static Boolean ToBoolean(Char[] value) {
              if ((value == null) || (value.Length == 0) ||
                  (value.Length == 1 && (value[0] == '\0' || value[0] == '0'))) {
                  return false;
              }
              String s = ToString(value);
              if ("false".Equals(s) ||
                  "false".Equals(s.Trim(m_trimmableChars).ToLower(CultureInfo.InvariantCulture))) {
                  return false;
              }
              return true;
          }
  
          public static Boolean ToBoolean(DateTime value) {
              throw new InvalidCastException("Invalid cast from 'DateTime' to 'Boolean'.");
          }
  
          public static Boolean ToBoolean(Decimal value) {
              return (value != 0M);
          }
  
          public static Boolean ToBoolean(Double value) {
              return (value != 0.0);
          }
  
          public static Boolean ToBoolean(Int16 value) {
              return (value != 0);
          }
  
          public static Boolean ToBoolean(Int32 value) {
              return (value != 0);
          }
  
          public static Boolean ToBoolean(Int64 value) {
              return (value != 0L);
          }
  
          public static Boolean ToBoolean(SByte value) {
              return (value != 0);
          }
  
          public static Boolean ToBoolean(Single value) {
              return (value != 0f);
          }
  
          public static Boolean ToBoolean(String value) {
              if ((value == null) || value.Equals("") || value.Equals("0") || "false".Equals(value) ||
                  "false".Equals(value.Trim(m_trimmableChars).ToLower(CultureInfo.InvariantCulture))) {
                  return false;
              }
              return true;
          }
  
          public static Boolean ToBoolean(StringBuilder value) {
              if (value == null) {
                  return false;
              }
              return ToBoolean(value.ToString());
          }
  
          public static Boolean ToBoolean(UInt16 value) {
              return (value != 0);
          }
  
          public static Boolean ToBoolean(UInt32 value) {
              return (value != 0);
          }
  
          public static Boolean ToBoolean(UInt64 value) {
              return (value != 0L);
          }
  
          public static Boolean ToBoolean(Object value) {
              if (value == null) {
                  return false;
              }
              if (value is Boolean) {
                  return (Boolean)value;
              }
              if (value is Byte) {
                  return ToBoolean((Byte)value);
              }
              if (value is Byte[]) {
                  return ToBoolean(ToString((Byte[])value));
              }
              if (value is Char) {
                  return ToBoolean((Char)value);
              }
              if (value is Char[]) {
                  return ToBoolean(ToString((Char[])value));
              }
              if (value is DateTime) {
                  return ToBoolean((DateTime)value);
              }
              if (value is Decimal) {
                  return ToBoolean((Decimal)value);
              }
              if (value is Double) {
                  return ToBoolean((Double)value);
              }
              if (value is Int16) {
                  return ToBoolean((Int16)value);
              }
              if (value is Int32) {
                  return ToBoolean((Int32)value);
              }
              if (value is Int64) {
                  return ToBoolean((Int64)value);
              }
              if (value is SByte) {
                  return ToBoolean((SByte)value);
              }
              if (value is Single) {
                  return ToBoolean((Single)value);
              }
              if (value is String) {
                  return ToBoolean((String)value);
              }
              if (value is StringBuilder) {
                  return ToBoolean(value.ToString());
              }
              if (value is UInt16) {
                  return ToBoolean((UInt16)value);
              }
              if (value is UInt32) {
                  return ToBoolean((UInt32)value);
              }
              if (value is UInt64) {
                  return ToBoolean((UInt64)value);
              }
              return ((IConvertible)value).ToBoolean(null);
          }
  
          #endregion
  
          #region ToBigInteger
  
          public static BigInteger ToBigInteger(BigInteger value) {
              return value;
          }
  
          public static BigInteger ToBigInteger(Byte value) {
              return (BigInteger)(UInt32)(value);
          }
  
          public static BigInteger ToBigInteger(Byte[] value) {
              if (value == null) {
                  return new BigInteger(0);
              }
              return BigInteger.Parse(ToString(value));
          }
  
          public static BigInteger ToBigInteger(Boolean value) {
              return (BigInteger)(UInt32)(value ? 1 : 0);
          }
  
          public static BigInteger ToBigInteger(Char value) {
              return (BigInteger)(UInt32)value;
          }
  
          public static BigInteger ToBigInteger(Char[] value) {
              return BigInteger.Parse(ToString(value));
          }
  
          public static BigInteger ToBigInteger(DateTime value) {
              throw new InvalidCastException("Invalid cast from 'DateTime' to 'BigInteger'.");
          }
  
          public static BigInteger ToBigInteger(Decimal value) {
              return BigInteger.Parse(value.ToString("#"));
          }
  
          public static BigInteger ToBigInteger(Double value) {
              return BigInteger.Parse(value.ToString("#"));
          }
  
          public static BigInteger ToBigInteger(Int16 value) {
              return BigInteger.Parse(value.ToString("#"));
          }
  
          public static BigInteger ToBigInteger(Int32 value) {
              return BigInteger.Parse(value.ToString("#"));
          }
  
          public static BigInteger ToBigInteger(Int64 value) {
              return BigInteger.Parse(value.ToString("#"));
          }
  
          public static BigInteger ToBigInteger(SByte value) {
              return BigInteger.Parse(value.ToString("#"));
          }
  
          public static BigInteger ToBigInteger(Single value) {
              return BigInteger.Parse(value.ToString("#"));
          }
  
          public static BigInteger ToBigInteger(String value) {
              if (value == null) {
                  return new BigInteger(0);
              }
              return BigInteger.Parse(value);
          }
  
          public static BigInteger ToBigInteger(StringBuilder value) {
              if (value == null) {
                  return new BigInteger(0);
              }
              return BigInteger.Parse(value.ToString());
          }
  
          public static BigInteger ToBigInteger(UInt16 value) {
              return (BigInteger)(UInt32)value;
          }
  
          public static BigInteger ToBigInteger(UInt32 value) {
              return (BigInteger)value;
          }
  
          public static BigInteger ToBigInteger(UInt64 value) {
              return (BigInteger)value;
          }
  
          public static BigInteger ToBigInteger(Object value) {
              if (value == null) {
                  return new BigInteger(0);
              }
              if (value is BigInteger) {
                  return (BigInteger)value;
              }
              if (value is Byte) {
                  return (BigInteger)(UInt32)(Byte)value;
              }
              if (value is Boolean) {
                  return (BigInteger)(UInt32)((Boolean)value ? 1 : 0);
              }
              if (value is Byte[]) {
                  return BigInteger.Parse(ToString((Byte[])value));
              }
              if (value is Char) {
                  return (BigInteger)(UInt32)(Char)value;
              }
              if (value is Char[]) {
                  return BigInteger.Parse(ToString((Char[])value));
              }
              if (value is DateTime) {
                  return ToBigInteger((DateTime)value);
              }
              if (value is Decimal) {
                  return BigInteger.Parse(((Decimal)value).ToString("#"));
              }
              if (value is Double) {
                  return BigInteger.Parse(((Double)value).ToString("#"));
              }
              if (value is Int16) {
                  return BigInteger.Parse(((Int16)value).ToString("#"));
              }
              if (value is Int32) {
                  return BigInteger.Parse(((Int32)value).ToString("#"));
              }
              if (value is Int64) {
                  return BigInteger.Parse(((Int64)value).ToString("#"));
              }
              if (value is SByte) {
                  return BigInteger.Parse(((SByte)value).ToString("#"));
              }
              if (value is Single) {
                  return BigInteger.Parse(((Single)value).ToString("#"));
              }
              if (value is String) {
                  return BigInteger.Parse((String)value);
              }
              if (value is StringBuilder) {
                  return BigInteger.Parse(value.ToString());
              }
              if (value is UInt16) {
                  return (BigInteger)(UInt32)(UInt16)value;
              }
              if (value is UInt32) {
                  return (BigInteger)(UInt32)value;
              }
              if (value is UInt64) {
                  return (BigInteger)(UInt64)value;
              }
              return BigInteger.Parse(((IConvertible)value).ToString(null));
          }
  
          #endregion
  
          #region ToByte
  
          public static Byte ToByte(Byte value) {
              return value;
          }
  
          public static Byte ToByte(Boolean value) {
              return (Byte)(value ? 1 : 0);
          }
  
          public static Byte ToByte(Byte[] value) {
              return ToByte(ToString(value));
          }
  
          public static Byte ToByte(Char value) {
              return (Byte)value;
          }
  
          public static Byte ToByte(Char[] value) {
              return ToByte(ToString(value));
          }
  
          public static Byte ToByte(DateTime value) {
              throw new InvalidCastException("Invalid cast from 'DateTime' to 'Byte'.");
          }
  
          public static Byte ToByte(Decimal value) {
              return (Byte)value;
          }
  
          public static Byte ToByte(Double value) {
              return (Byte)value;
          }
  
          public static Byte ToByte(Int16 value) {
              return (Byte)value;
          }
  
          public static Byte ToByte(Int32 value) {
              return (Byte)value;
          }
  
          public static Byte ToByte(Int64 value) {
              return (Byte)value;
          }
  
          public static Byte ToByte(SByte value) {
              return (Byte)value;
          }
  
          public static Byte ToByte(Single value) {
              return (Byte)value;
          }
  
          public static Byte ToByte(String value) {
              if (value == null) {
                  return 0;
              }
              return Byte.Parse(value);
          }
  
          public static Byte ToByte(StringBuilder value) {
              if (value == null) {
                  return 0;
              }
              return Byte.Parse(value.ToString());
          }
  
          public static Byte ToByte(UInt16 value) {
              return (Byte)value;
          }
  
          public static Byte ToByte(UInt32 value) {
              return (Byte)value;
          }
  
          public static Byte ToByte(UInt64 value) {
              return (Byte)value;
          }
  
          public static Byte ToByte(Object value) {
              if (value == null) {
                  return 0;
              }
              if (value is Byte) {
                  return (Byte)value;
              }
              if (value is Boolean) {
                  return (Byte)((Boolean)value ? 1 : 0);
              }
              if (value is Byte[]) {
                  return ToByte(ToString((Byte[])value));
              }
              if (value is Char) {
                  return (Byte)(Char)value;
              }
              if (value is Char[]) {
                  return ToByte(ToString((Char[])value));
              }
              if (value is DateTime) {
                  return ToByte((DateTime)value);
              }
              if (value is Decimal) {
                  return (Byte)(Decimal)value;
              }
              if (value is Double) {
                  return (Byte)(Double)value;
              }
              if (value is Int16) {
                  return (Byte)(Int16)value;
              }
              if (value is Int32) {
                  return (Byte)(Int32)value;
              }
              if (value is Int64) {
                  return (Byte)(Int64)value;
              }
              if (value is SByte) {
                  return (Byte)(SByte)value;
              }
              if (value is Single) {
                  return (Byte)(Single)value;
              }
              if (value is String) {
                  return Byte.Parse((String)value);
              }
              if (value is StringBuilder) {
                  return Byte.Parse(value.ToString());
              }
              if (value is UInt16) {
                  return (Byte)(UInt16)value;
              }
              if (value is UInt32) {
                  return (Byte)(UInt32)value;
              }
              if (value is UInt64) {
                  return (Byte)(UInt64)value;
              }
              return ((IConvertible)value).ToByte(null);
          }
  
          #endregion
  
          #region ToChar
  
          public static Char ToChar(Char value) {
              return value;
          }
  
          public static Char ToChar(Boolean value) {
              return (value ? '1' : '0');
          }
  
          public static Char ToChar(Byte value) {
              return (Char)value;
          }
  
          public static Char ToChar(Byte[] value) {
              if (value == null || value.Length == 0) {
                  return '\0';
              }
              return ToString(value)[0];
          }
  
          public static Char ToChar(Char[] value) {
              if (value == null || value.Length == 0) {
                  return '\0';
              }
              return value[0];
          }
  
          public static Char ToChar(DateTime value) {
              throw new InvalidCastException("Invalid cast from 'DateTime' to 'Char'.");
          }
  
          public static Char ToChar(Decimal value) {
              return (Char)(Int16)value;
          }
  
          public static Char ToChar(Double value) {
              return (Char)value;
          }
  
          public static Char ToChar(Int16 value) {
              return (Char)value;
          }
  
          public static Char ToChar(Int32 value) {
              return (Char)value;
          }
  
          public static Char ToChar(Int64 value) {
              return (Char)value;
          }
  
          public static Char ToChar(SByte value) {
              return (Char)value;
          }
  
          public static Char ToChar(Single value) {
              return (Char)value;
          }
  
          public static Char ToChar(String value) {
              if (value == null || value == "") {
                  return '\0';
              }
              return value[0];
          }
  
          public static Char ToChar(StringBuilder value) {
              if (value == null || value.Length == 0) {
                  return '\0';
              }
              return value.ToString()[0];
          }
  
          public static Char ToChar(UInt16 value) {
              return (Char)value;
          }
  
          public static Char ToChar(UInt32 value) {
              return (Char)value;
          }
  
          public static Char ToChar(UInt64 value) {
              return (Char)value;
          }
  
          public static Char ToChar(Object value) {
              if (value == null) {
                  return '\0';
              }
              if (value is Char) {
                  return (Char)value;
              }
              if (value is Boolean) {
                  return ((Boolean)value ? '1' : '0');
              }
              if (value is Byte) {
                  return (Char)(Byte)value;
              }
              if (value is Byte[]) {
                  return ToChar((Byte[])value);
              }
              if (value is Char[]) {
                  return ToChar((Char[])value);
              }
              if (value is DateTime) {
                  return ToChar((DateTime)value);
              }
              if (value is Decimal) {
                  return (Char)(Int16)(Decimal)value;
              }
              if (value is Double) {
                  return (Char)(Double)value;
              }
              if (value is Int16) {
                  return (Char)(Int16)value;
              }
              if (value is Int32) {
                  return (Char)(Int32)value;
              }
              if (value is Int64) {
                  return (Char)(Int64)value;
              }
              if (value is SByte) {
                  return (Char)(SByte)value;
              }
              if (value is Single) {
                  return (Char)(Single)value;
              }
              if (value is String) {
                  return ToChar((String)value);
              }
              if (value is StringBuilder) {
                  return ToChar(value.ToString());
              }
              if (value is UInt16) {
                  return (Char)(UInt16)value;
              }
              if (value is UInt32) {
                  return (Char)(UInt32)value;
              }
              if (value is UInt64) {
                  return (Char)(UInt64)value;
              }
              return ((IConvertible)value).ToChar(null);
          }
  
          #endregion
  
          #region ToDateTime
  
          public static DateTime ToDateTime(Boolean value) {
              throw new InvalidCastException("Invalid cast from 'Boolean' to 'DateTime'.");
          }
  
          public static DateTime ToDateTime(Byte value) {
              throw new InvalidCastException("Invalid cast from 'Byte' to 'DateTime'.");
          }
  
          public static DateTime ToDateTime(Byte[] value) {
              if (value == null) {
                  return DateTime.MinValue;
              }
              return DateTime.Parse(ToString(value));
          }
  
          public static DateTime ToDateTime(Char value) {
              throw new InvalidCastException("Invalid cast from 'Char' to 'DateTime'.");
          }
  
          public static DateTime ToDateTime(Char[] value) {
              if (value == null) {
                  return DateTime.MinValue;
              }
              return DateTime.Parse(ToString(value));
          }
  
          public static DateTime ToDateTime(DateTime value) {
              return value;
          }
  
          public static DateTime ToDateTime(Decimal value) {
              throw new InvalidCastException("Invalid cast from 'Decimal' to 'DateTime'.");
          }
  
          public static DateTime ToDateTime(Double value) {
              throw new InvalidCastException("Invalid cast from 'Double' to 'DateTime'.");
          }
  
          public static DateTime ToDateTime(Int16 value) {
              throw new InvalidCastException("Invalid cast from 'Int16' to 'DateTime'.");
          }
  
          public static DateTime ToDateTime(Int32 value) {
              throw new InvalidCastException("Invalid cast from 'Int32' to 'DateTime'.");
          }
  
          public static DateTime ToDateTime(Int64 value) {
              throw new InvalidCastException("Invalid cast from 'Int64' to 'DateTime'.");
          }
  
          public static DateTime ToDateTime(SByte value) {
              throw new InvalidCastException("Invalid cast from 'SByte' to 'DateTime'.");
          }
  
          public static DateTime ToDateTime(Single value) {
              throw new InvalidCastException("Invalid cast from 'Single' to 'DateTime'.");
          }
  
          public static DateTime ToDateTime(String value) {
              if (value == null) {
                  return DateTime.MinValue;
              }
              return DateTime.Parse(value);
          }
  
          public static DateTime ToDateTime(StringBuilder value) {
              if (value == null) {
                  return DateTime.MinValue;
              }
              return DateTime.Parse(value.ToString());
          }
  
          public static DateTime ToDateTime(UInt16 value) {
              throw new InvalidCastException("Invalid cast from 'UInt16' to 'DateTime'.");
          }
  
          public static DateTime ToDateTime(UInt32 value) {
              throw new InvalidCastException("Invalid cast from 'UInt32' to 'DateTime'.");
          }
  
          public static DateTime ToDateTime(UInt64 value) {
              throw new InvalidCastException("Invalid cast from 'UInt64' to 'DateTime'.");
          }
  
          public static DateTime ToDateTime(Object value) {
              if (value == null) {
                  return DateTime.MinValue;
              }
              if (value is Byte[]) {
                  return DateTime.Parse(ToString((Byte[])value));
              }
              if (value is Char[]) {
                  return DateTime.Parse(ToString((Char[])value));
              }
              if (value is String) {
                  return DateTime.Parse((String)value);
              }
              if (value is StringBuilder) {
                  return DateTime.Parse(value.ToString());
              }
              return ((IConvertible)value).ToDateTime(provider);
          }
  
          #endregion
  
          #region ToDecimal
  
          public static Decimal ToDecimal(Decimal value) {
              return value;
          }
  
          public static Decimal ToDecimal(Boolean value) {
              return (Decimal)(value ? 1 : 0);
          }
  
          public static Decimal ToDecimal(Byte value) {
              return (Decimal)value;
          }
  
          public static Decimal ToDecimal(Byte[] value) {
              return ToDecimal(ToString(value));
          }
  
          public static Decimal ToDecimal(Char value) {
              return (Decimal)value;
          }
  
          public static Decimal ToDecimal(Char[] value) {
              return ToDecimal(ToString(value));
          }
  
          public static Decimal ToDecimal(DateTime value) {
              throw new InvalidCastException("Invalid cast from 'DateTime' to 'Decimal'.");
          }
  
          public static Decimal ToDecimal(Double value) {
              return (Decimal)value;
          }
  
          public static Decimal ToDecimal(Int16 value) {
              return (Decimal)value;
          }
  
          public static Decimal ToDecimal(Int32 value) {
              return (Decimal)value;
          }
  
          public static Decimal ToDecimal(Int64 value) {
              return (Decimal)value;
          }
  
          public static Decimal ToDecimal(SByte value) {
              return (Decimal)value;
          }
  
          public static Decimal ToDecimal(Single value) {
              return (Decimal)value;
          }
  
          public static Decimal ToDecimal(String value) {
              if (value == null) {
                  return 0;
              }
              return Decimal.Parse(value);
          }
  
          public static Decimal ToDecimal(StringBuilder value) {
              if (value == null) {
                  return 0;
              }
              return Decimal.Parse(value.ToString());
          }
  
          public static Decimal ToDecimal(UInt16 value) {
              return (Decimal)value;
          }
  
          public static Decimal ToDecimal(UInt32 value) {
              return (Decimal)value;
          }
  
          public static Decimal ToDecimal(UInt64 value) {
              return (Decimal)value;
          }
  
          public static Decimal ToDecimal(Object value) {
              if (value == null) {
                  return 0;
              }
              if (value is Decimal) {
                  return (Decimal)value;
              }
              if (value is Boolean) {
                  return (Decimal)((Boolean)value ? 1 : 0);
              }
              if (value is Byte) {
                  return (Decimal)(Byte)value;
              }
              if (value is Byte[]) {
                  return ToDecimal(ToString((Byte[])value));
              }
              if (value is Char) {
                  return (Decimal)(Char)value;
              }
              if (value is Char[]) {
                  return ToDecimal(ToString((Char[])value));
              }
              if (value is DateTime) {
                  return ToDecimal((DateTime)value);
              }
              if (value is Double) {
                  return (Decimal)(Double)value;
              }
              if (value is Int16) {
                  return (Decimal)(Int16)value;
              }
              if (value is Int32) {
                  return (Decimal)(Int32)value;
              }
              if (value is Int64) {
                  return (Decimal)(Int64)value;
              }
              if (value is SByte) {
                  return (Decimal)(SByte)value;
              }
              if (value is Single) {
                  return (Decimal)(Single)value;
              }
              if (value is String) {
                  return Decimal.Parse((String)value);
              }
              if (value is StringBuilder) {
                  return Decimal.Parse(value.ToString());
              }
              if (value is UInt16) {
                  return (Decimal)(UInt16)value;
              }
              if (value is UInt32) {
                  return (Decimal)(UInt32)value;
              }
              if (value is UInt64) {
                  return (Decimal)(UInt64)value;
              }
              return ((IConvertible)value).ToDecimal(null);
          }
  
          #endregion
  
          #region ToDouble
  
          public static Double ToDouble(Double value) {
              return value;
          }
  
          public static Double ToDouble(Boolean value) {
              return (Double)(value ? 1 : 0);
          }
  
          public static Double ToDouble(Byte value) {
              return (Double)value;
          }
  
          public static Double ToDouble(Byte[] value) {
              return ToDouble(ToString(value));
          }
  
          public static Double ToDouble(Char value) {
              return (Double)value;
          }
  
          public static Double ToDouble(Char[] value) {
              return ToDouble(ToString(value));
          }
  
          public static Double ToDouble(DateTime value) {
              throw new InvalidCastException("Invalid cast from 'DateTime' to 'Double'.");
          }
  
          public static Double ToDouble(Decimal value) {
              return (Double)value;
          }
  
          public static Double ToDouble(Int16 value) {
              return (Double)value;
          }
  
          public static Double ToDouble(Int32 value) {
              return (Double)value;
          }
  
          public static Double ToDouble(Int64 value) {
              return (Double)value;
          }
  
          public static Double ToDouble(SByte value) {
              return (Double)value;
          }
  
          public static Double ToDouble(Single value) {
              return (Double)value;
          }
  
          public static Double ToDouble(String value) {
              if (value == null) {
                  return 0;
              }
              return Double.Parse(value);
          }
  
          public static Double ToDouble(StringBuilder value) {
              if (value == null) {
                  return 0;
              }
              return Double.Parse(value.ToString());
          }
  
          public static Double ToDouble(UInt16 value) {
              return (Double)value;
          }
  
          public static Double ToDouble(UInt32 value) {
              return (Double)value;
          }
  
          public static Double ToDouble(UInt64 value) {
              return (Double)value;
          }
  
          public static Double ToDouble(Object value) {
              if (value == null) {
                  return 0;
              }
              if (value is Double) {
                  return (Double)value;
              }
              if (value is Boolean) {
                  return (Double)((Boolean)value ? 1 : 0);
              }
              if (value is Byte) {
                  return (Double)(Byte)value;
              }
              if (value is Byte[]) {
                  return ToDouble(ToString((Byte[])value));
              }
              if (value is Char) {
                  return (Double)(Char)value;
              }
              if (value is Char[]) {
                  return ToDouble(ToString((Char[])value));
              }
              if (value is DateTime) {
                  return ToDouble((DateTime)value);
              }
              if (value is Decimal) {
                  return (Double)(Decimal)value;
              }
              if (value is Int16) {
                  return (Double)(Int16)value;
              }
              if (value is Int32) {
                  return (Double)(Int32)value;
              }
              if (value is Int64) {
                  return (Double)(Int64)value;
              }
              if (value is SByte) {
                  return (Double)(SByte)value;
              }
              if (value is Single) {
                  return (Double)(Single)value;
              }
              if (value is String) {
                  return Double.Parse((String)value);
              }
              if (value is StringBuilder) {
                  return Double.Parse(value.ToString());
              }
              if (value is UInt16) {
                  return (Double)(UInt16)value;
              }
              if (value is UInt32) {
                  return (Double)(UInt32)value;
              }
              if (value is UInt64) {
                  return (Double)(UInt64)value;
              }
              return ((IConvertible)value).ToDouble(null);
          }
  
          #endregion
  
          #region ToEnum
  
          public static Object ToEnum(Byte value, Type conversionType) {
              return Enum.ToObject(conversionType, value);
          }
  
          public static Object ToEnum(Boolean value, Type conversionType) {
              return Enum.ToObject(conversionType, (Byte)(value ? 1 : 0));
          }
  
          public static Object ToEnum(Char value, Type conversionType) {
              return Enum.ToObject(conversionType, (Int32)value);
          }
  
          public static Object ToEnum(DateTime value, Type conversionType) {
              throw new InvalidCastException("Invalid cast from 'DateTime' to '" + conversionType.Name + "'.");
          }
  
          public static Object ToEnum(Decimal value, Type conversionType) {
              return Enum.ToObject(conversionType, (UInt64)value);
          }
  
          public static Object ToEnum(Double value, Type conversionType) {
              return Enum.ToObject(conversionType, (UInt64)value);
          }
  
          public static Object ToEnum(Int16 value, Type conversionType) {
              return Enum.ToObject(conversionType, value);
          }
  
          public static Object ToEnum(Int32 value, Type conversionType) {
              return Enum.ToObject(conversionType, value);
          }
  
          public static Object ToEnum(Int64 value, Type conversionType) {
              return Enum.ToObject(conversionType, value);
          }
  
          public static Object ToEnum(SByte value, Type conversionType) {
              return Enum.ToObject(conversionType, value);
          }
  
          public static Object ToEnum(Single value, Type conversionType) {
              return Enum.ToObject(conversionType, (UInt64)value);
          }
  
          public static Object ToEnum(UInt16 value, Type conversionType) {
              return Enum.ToObject(conversionType, value);
          }
  
          public static Object ToEnum(UInt32 value, Type conversionType) {
              return Enum.ToObject(conversionType, value);
          }
  
          public static Object ToEnum(UInt64 value, Type conversionType) {
              return Enum.ToObject(conversionType, value);
          }
  
  #if !(NETCF1)
          public static Object ToEnum(Byte[] value, Type conversionType) {
              return Enum.Parse(conversionType, ToString(value), true);
          }
  
          public static Object ToEnum(Char[] value, Type conversionType) {
              return Enum.Parse(conversionType, ToString(value), true);
          }
  
          public static Object ToEnum(String value, Type conversionType) {
              if (value == null) {
                  throw new InvalidCastException("Null object cannot be converted to a value type.");
              }
              return Enum.Parse(conversionType, value, true);
          }
  
          public static Object ToEnum(StringBuilder value, Type conversionType) {
              if (value == null) {
                  throw new InvalidCastException("Null object cannot be converted to a value type.");
              }
              return Enum.Parse(conversionType, value.ToString(), true);
          }
  #endif
  
          public static Object ToEnum(Object value, Type conversionType) {
              if (value == null) {
                  throw new InvalidCastException("Null object cannot be converted to a value type.");
              }
              if (value.GetType() == conversionType) {
                  return value;
              }
              if ((value is Byte) ||
                  (value is SByte) ||
                  (value is Int16) ||
                  (value is Int32) ||
                  (value is Int64) ||
                  (value is UInt16) ||
                  (value is UInt32) ||
                  (value is UInt64)) {
                  return Enum.ToObject(conversionType, value);
              }
              if (value is Boolean) {
                  return Enum.ToObject(conversionType, (Byte)((Boolean)value ? 1 : 0));
              }
              if (value is Char) {
                  return Enum.ToObject(conversionType, (Int32)(Char)value);
              }
              if (value is DateTime) {
                  return ToEnum((DateTime)value, conversionType);
              }
              if (value is Decimal) {
                  return Enum.ToObject(conversionType, (UInt64)(Decimal)value);
              }
              if (value is Double) {
                  return Enum.ToObject(conversionType, (UInt64)(Double)value);
              }
              if (value is Single) {
                  return Enum.ToObject(conversionType, (UInt64)(Single)value);
              }
  #if !(NETCF1)
              if (value is Byte[]) {
                  return Enum.Parse(conversionType, ToString((Byte[])value), true);
              }
              if (value is Char[]) {
                  return Enum.Parse(conversionType, ToString((Char[])value), true);
              }
              if (value is String) {
                  return Enum.Parse(conversionType, (String)value, true);
              }
              if (value is StringBuilder) {
                  return Enum.Parse(conversionType, value.ToString(), true);
              }
  #endif
              return Enum.ToObject(conversionType, ((IConvertible)value).ToUInt64(null));
          }
  
          #endregion
  
          #region ToInt16
  
          public static Int16 ToInt16(Int16 value) {
              return value;
          }
  
          public static Int16 ToInt16(Boolean value) {
              return (Int16)(value ? 1 : 0);
          }
  
          public static Int16 ToInt16(Byte value) {
              return (Int16)value;
          }
  
          public static Int16 ToInt16(Byte[] value) {
              return ToInt16(ToString(value));
          }
  
          public static Int16 ToInt16(Char value) {
              return (Int16)value;
          }
  
          public static Int16 ToInt16(Char[] value) {
              return ToInt16(ToString(value));
          }
  
          public static Int16 ToInt16(DateTime value) {
              throw new InvalidCastException("Invalid cast from 'DateTime' to 'Int16'.");
          }
  
          public static Int16 ToInt16(Decimal value) {
              return (Int16)value;
          }
  
          public static Int16 ToInt16(Double value) {
              return (Int16)value;
          }
  
          public static Int16 ToInt16(Int32 value) {
              return (Int16)value;
          }
  
          public static Int16 ToInt16(Int64 value) {
              return (Int16)value;
          }
  
          public static Int16 ToInt16(SByte value) {
              return (Int16)value;
          }
  
          public static Int16 ToInt16(Single value) {
              return (Int16)value;
          }
  
          public static Int16 ToInt16(String value) {
              if (value == null) {
                  return 0;
              }
              return Int16.Parse(value);
          }
  
          public static Int16 ToInt16(StringBuilder value) {
              if (value == null) {
                  return 0;
              }
              return Int16.Parse(value.ToString());
          }
  
          public static Int16 ToInt16(UInt16 value) {
              return (Int16)value;
          }
  
          public static Int16 ToInt16(UInt32 value) {
              return (Int16)value;
          }
  
          public static Int16 ToInt16(UInt64 value) {
              return (Int16)value;
          }
  
          public static Int16 ToInt16(Object value) {
              if (value == null) {
                  return 0;
              }
              if (value is Int16) {
                  return (Int16)value;
              }
              if (value is Boolean) {
                  return (Int16)((Boolean)value ? 1 : 0);
              }
              if (value is Byte) {
                  return (Int16)(Byte)value;
              }
              if (value is Byte[]) {
                  return ToInt16(ToString((Byte[])value));
              }
              if (value is Char) {
                  return (Int16)(Char)value;
              }
              if (value is Char[]) {
                  return ToInt16(ToString((Char[])value));
              }
              if (value is DateTime) {
                  return ToInt16((DateTime)value);
              }
              if (value is Decimal) {
                  return (Int16)(Decimal)value;
              }
              if (value is Double) {
                  return (Int16)(Double)value;
              }
              if (value is Int32) {
                  return (Int16)(Int32)value;
              }
              if (value is Int64) {
                  return (Int16)(Int64)value;
              }
              if (value is SByte) {
                  return (Int16)(SByte)value;
              }
              if (value is Single) {
                  return (Int16)(Single)value;
              }
              if (value is String) {
                  return Int16.Parse((String)value);
              }
              if (value is StringBuilder) {
                  return Int16.Parse(value.ToString());
              }
              if (value is UInt16) {
                  return (Int16)(UInt16)value;
              }
              if (value is UInt32) {
                  return (Int16)(UInt32)value;
              }
              if (value is UInt64) {
                  return (Int16)(UInt64)value;
              }
              return ((IConvertible)value).ToInt16(null);
          }
  
          #endregion
  
          #region ToInt32
  
          public static Int32 ToInt32(Int32 value) {
              return value;
          }
  
          public static Int32 ToInt32(Boolean value) {
              return (Int32)(value ? 1 : 0);
          }
  
          public static Int32 ToInt32(Byte value) {
              return (Int32)value;
          }
  
          public static Int32 ToInt32(Byte[] value) {
              return ToInt32(ToString(value));
          }
  
          public static Int32 ToInt32(Char value) {
              return (Int32)value;
          }
  
          public static Int32 ToInt32(Char[] value) {
              return ToInt32(ToString(value));
          }
  
          public static Int32 ToInt32(DateTime value) {
              throw new InvalidCastException("Invalid cast from 'DateTime' to 'Int32'.");
          }
  
          public static Int32 ToInt32(Decimal value) {
              return (Int32)value;
          }
  
          public static Int32 ToInt32(Double value) {
              return (Int32)value;
          }
  
          public static Int32 ToInt32(Int16 value) {
              return (Int32)value;
          }
  
          public static Int32 ToInt32(Int64 value) {
              return (Int32)value;
          }
  
          public static Int32 ToInt32(SByte value) {
              return (Int32)value;
          }
  
          public static Int32 ToInt32(Single value) {
              return (Int32)value;
          }
  
          public static Int32 ToInt32(String value) {
              if (value == null) {
                  return 0;
              }
              return Int32.Parse(value);
          }
  
          public static Int32 ToInt32(StringBuilder value) {
              if (value == null) {
                  return 0;
              }
              return Int32.Parse(value.ToString());
          }
  
          public static Int32 ToInt32(UInt16 value) {
              return (Int32)value;
          }
  
          public static Int32 ToInt32(UInt32 value) {
              return (Int32)value;
          }
  
          public static Int32 ToInt32(UInt64 value) {
              return (Int32)value;
          }
  
          public static Int32 ToInt32(Object value) {
              if (value == null) {
                  return 0;
              }
              if (value is Int32) {
                  return (Int32)value;
              }
              if (value is Boolean) {
                  return (Int32)((Boolean)value ? 1 : 0);
              }
              if (value is Byte) {
                  return (Int32)(Byte)value;
              }
              if (value is Byte[]) {
                  return ToInt32(ToString((Byte[])value));
              }
              if (value is Char) {
                  return (Int32)(Char)value;
              }
              if (value is Char[]) {
                  return ToInt32(ToString((Char[])value));
              }
              if (value is DateTime) {
                  return ToInt32((DateTime)value);
              }
              if (value is Decimal) {
                  return (Int32)(Decimal)value;
              }
              if (value is Double) {
                  return (Int32)(Double)value;
              }
              if (value is Int16) {
                  return (Int32)(Int16)value;
              }
              if (value is Int64) {
                  return (Int32)(Int64)value;
              }
              if (value is SByte) {
                  return (Int32)(SByte)value;
              }
              if (value is Single) {
                  return (Int32)(Single)value;
              }
              if (value is String) {
                  return Int32.Parse((String)value);
              }
              if (value is StringBuilder) {
                  return Int32.Parse(value.ToString());
              }
              if (value is UInt16) {
                  return (Int32)(UInt16)value;
              }
              if (value is UInt32) {
                  return (Int32)(UInt32)value;
              }
              if (value is UInt64) {
                  return (Int32)(UInt64)value;
              }
              return ((IConvertible)value).ToInt32(null);
          }
  
           #endregion
  
          #region ToInt64
  
          public static Int64 ToInt64(Int64 value) {
              return value;
          }
  
          public static Int64 ToInt64(Boolean value) {
              return (Int64)(value ? 1 : 0);
          }
  
          public static Int64 ToInt64(Byte value) {
              return (Int64)value;
          }
  
          public static Int64 ToInt64(Byte[] value) {
              return ToInt64(ToString(value));
          }
  
          public static Int64 ToInt64(Char value) {
              return (Int64)value;
          }
  
          public static Int64 ToInt64(Char[] value) {
              return ToInt64(ToString(value));
          }
  
          public static Int64 ToInt64(DateTime value) {
              throw new InvalidCastException("Invalid cast from 'DateTime' to 'Int64'.");
          }
  
          public static Int64 ToInt64(Decimal value) {
              return (Int64)value;
          }
  
          public static Int64 ToInt64(Double value) {
              return (Int64)value;
          }
  
          public static Int64 ToInt64(Int16 value) {
              return (Int64)value;
          }
  
          public static Int64 ToInt64(Int32 value) {
              return (Int64)value;
          }
  
          public static Int64 ToInt64(SByte value) {
              return (Int64)value;
          }
  
          public static Int64 ToInt64(Single value) {
              return (Int64)value;
          }
  
          public static Int64 ToInt64(String value) {
              if (value == null) {
                  return 0;
              }
              return Int64.Parse(value);
          }
  
          public static Int64 ToInt64(StringBuilder value) {
              if (value == null) {
                  return 0;
              }
              return Int64.Parse(value.ToString());
          }
  
          public static Int64 ToInt64(UInt16 value) {
              return (Int64)value;
          }
  
          public static Int64 ToInt64(UInt32 value) {
              return (Int64)value;
          }
  
          public static Int64 ToInt64(UInt64 value) {
              return (Int64)value;
          }
  
          public static Int64 ToInt64(Object value) {
              if (value == null) {
                  return 0;
              }
              if (value is Int64) {
                  return (Int64)value;
              }
              if (value is Boolean) {
                  return (Int64)((Boolean)value ? 1 : 0);
              }
              if (value is Byte) {
                  return (Int64)(Byte)value;
              }
              if (value is Byte[]) {
                  return ToInt64(ToString((Byte[])value));
              }
              if (value is Char) {
                  return (Int64)(Char)value;
              }
              if (value is Char[]) {
                  return ToInt64(ToString((Char[])value));
              }
              if (value is DateTime) {
                  return ToInt64((DateTime)value);
              }
              if (value is Decimal) {
                  return (Int64)(Decimal)value;
              }
              if (value is Double) {
                  return (Int64)(Double)value;
              }
              if (value is Int16) {
                  return (Int64)(Int16)value;
              }
              if (value is Int32) {
                  return (Int64)(Int32)value;
              }
              if (value is SByte) {
                  return (Int64)(SByte)value;
              }
              if (value is Single) {
                  return (Int64)(Single)value;
              }
              if (value is String) {
                  return Int64.Parse((String)value);
              }
              if (value is StringBuilder) {
                  return Int64.Parse(value.ToString());
              }
              if (value is UInt16) {
                  return (Int64)(UInt16)value;
              }
              if (value is UInt32) {
                  return (Int64)(UInt32)value;
              }
              if (value is UInt64) {
                  return (Int64)(UInt64)value;
              }
              return ((IConvertible)value).ToInt64(null);
          }
  
           #endregion
  
          #region ToSByte
  
          public static SByte ToSByte(SByte value) {
              return value;
          }
  
          public static SByte ToSByte(Boolean value) {
              return (SByte)(value ? 1 : 0);
          }
  
          public static SByte ToSByte(Byte value) {
              return (SByte)value;
          }
  
          public static SByte ToSByte(Byte[] value) {
              return ToSByte(ToString(value));
          }
  
          public static SByte ToSByte(Char value) {
              return (SByte)value;
          }
  
          public static SByte ToSByte(Char[] value) {
              return ToSByte(ToString(value));
          }
  
          public static SByte ToSByte(DateTime value) {
              throw new InvalidCastException("Invalid cast from 'DateTime' to 'SByte'.");
          }
  
          public static SByte ToSByte(Decimal value) {
              return (SByte)value;
          }
  
          public static SByte ToSByte(Double value) {
              return (SByte)value;
          }
  
          public static SByte ToSByte(Int16 value) {
              return (SByte)value;
          }
  
          public static SByte ToSByte(Int32 value) {
              return (SByte)value;
          }
  
          public static SByte ToSByte(Int64 value) {
              return (SByte)value;
          }
  
          public static SByte ToSByte(Single value) {
              return (SByte)value;
          }
  
          public static SByte ToSByte(String value) {
              if (value == null) {
                  return 0;
              }
              return SByte.Parse(value);
          }
  
          public static SByte ToSByte(StringBuilder value) {
              if (value == null) {
                  return 0;
              }
              return SByte.Parse(value.ToString());
          }
  
          public static SByte ToSByte(UInt16 value) {
              return (SByte)value;
          }
  
          public static SByte ToSByte(UInt32 value) {
              return (SByte)value;
          }
  
          public static SByte ToSByte(UInt64 value) {
              return (SByte)value;
          }
  
          public static SByte ToSByte(Object value) {
              if (value == null) {
                  return 0;
              }
              if (value is SByte) {
                  return (SByte)value;
              }
              if (value is Boolean) {
                  return (SByte)((Boolean)value ? 1 : 0);
              }
              if (value is Byte) {
                  return (SByte)(Byte)value;
              }
              if (value is Byte[]) {
                  return ToSByte(ToString((Byte[])value));
              }
              if (value is Char) {
                  return (SByte)(Char)value;
              }
              if (value is Char[]) {
                  return ToSByte(ToString((Char[])value));
              }
              if (value is DateTime) {
                  return ToSByte((DateTime)value);
              }
              if (value is Decimal) {
                  return (SByte)(Decimal)value;
              }
              if (value is Double) {
                  return (SByte)(Double)value;
              }
              if (value is Int16) {
                  return (SByte)(Int16)value;
              }
              if (value is Int32) {
                  return (SByte)(Int32)value;
              }
              if (value is Int64) {
                  return (SByte)(Int64)value;
              }
              if (value is Single) {
                  return (SByte)(Single)value;
              }
              if (value is String) {
                  return SByte.Parse((String)value);
              }
              if (value is StringBuilder) {
                  return SByte.Parse(value.ToString());
              }
              if (value is UInt16) {
                  return (SByte)(UInt16)value;
              }
              if (value is UInt32) {
                  return (SByte)(UInt32)value;
              }
              if (value is UInt64) {
                  return (SByte)(UInt64)value;
              }
              return ((IConvertible)value).ToSByte(null);
          }
  
           #endregion
  
          #region ToSingle
  
          public static Single ToSingle(Single value) {
              return value;
          }
  
          public static Single ToSingle(Boolean value) {
              return (Single)(value ? 1 : 0);
          }
  
          public static Single ToSingle(Byte value) {
              return (Single)value;
          }
  
          public static Single ToSingle(Byte[] value) {
              return ToSingle(ToString(value));
          }
  
          public static Single ToSingle(Char value) {
              return (Single)value;
          }
  
          public static Single ToSingle(Char[] value) {
              return ToSingle(ToString(value));
          }
  
          public static Single ToSingle(DateTime value) {
              throw new InvalidCastException("Invalid cast from 'DateTime' to 'Single'.");
          }
  
          public static Single ToSingle(Decimal value) {
              return (Single)value;
          }
  
          public static Single ToSingle(Double value) {
              return (Single)value;
          }
  
          public static Single ToSingle(Int16 value) {
              return (Single)value;
          }
  
          public static Single ToSingle(Int32 value) {
              return (Single)value;
          }
  
          public static Single ToSingle(Int64 value) {
              return (Single)value;
          }
  
          public static Single ToSingle(SByte value) {
              return (Single)value;
          }
  
          public static Single ToSingle(String value) {
              if (value == null) {
                  return 0;
              }
              return Single.Parse(value);
          }
  
          public static Single ToSingle(StringBuilder value) {
              if (value == null) {
                  return 0;
              }
              return Single.Parse(value.ToString());
          }
  
          public static Single ToSingle(UInt16 value) {
              return (Single)value;
          }
  
          public static Single ToSingle(UInt32 value) {
              return (Single)value;
          }
  
          public static Single ToSingle(UInt64 value) {
              return (Single)value;
          }
  
          public static Single ToSingle(Object value) {
              if (value == null) {
                  return 0;
              }
              if (value is Single) {
                  return (Single)value;
              }
              if (value is Boolean) {
                  return (Single)((Boolean)value ? 1 : 0);
              }
              if (value is Byte) {
                  return (Single)(Byte)value;
              }
              if (value is Byte[]) {
                  return ToSingle(ToString((Byte[])value));
              }
              if (value is Char) {
                  return (Single)(Char)value;
              }
              if (value is Char[]) {
                  return ToSingle(ToString((Char[])value));
              }
              if (value is DateTime) {
                  return ToSingle((DateTime)value);
              }
              if (value is Decimal) {
                  return (Single)(Decimal)value;
              }
              if (value is Double) {
                  return (Single)(Double)value;
              }
              if (value is Int16) {
                  return (Single)(Int16)value;
              }
              if (value is Int32) {
                  return (Single)(Int32)value;
              }
              if (value is Int64) {
                  return (Single)(Int64)value;
              }
              if (value is SByte) {
                  return (Single)(SByte)value;
              }
              if (value is String) {
                  return Single.Parse((String)value);
              }
              if (value is StringBuilder) {
                  return Single.Parse(value.ToString());
              }
              if (value is UInt16) {
                  return (Single)(UInt16)value;
              }
              if (value is UInt32) {
                  return (Single)(UInt32)value;
              }
              if (value is UInt64) {
                  return (Single)(UInt64)value;
              }
              return ((IConvertible)value).ToSingle(null);
          }
  
          #endregion
  
          #region ToString
  
          public static String ToString(Byte[] value) {
              return UTF8.GetString(value, 0, value.GetLength(0));
          }
  
          public static String ToString(Byte[] value, String charset) {
              return Encoding.GetEncoding(charset).GetString(value, 0, value.GetLength(0));
          }
  
          public static String ToString(Byte[] value, Encoding encoding) {
              return encoding.GetString(value, 0, value.GetLength(0));
          }
  
          public static String ToString(Object value) {
              return ToString(value, UTF8);
          }
  
          public static String ToString(Object value, String charset) {
              if (value == null) {
                  return null;
              }
              if (value is String) {
                  return (String)value;
              }
              if (value is Char[]) {
                  return new String((Char[])value);
              }
              if (value is Byte[]) {
                  return ToString((Byte[])value, charset);
              }
              return value.ToString();
          }
  
          public static String ToString(Object value, Encoding encoding) {
              if (value == null) {
                  return null;
              }
              if (value is String) {
                  return (String)value;
              }
              if (value is Char[]) {
                  return new String((Char[])value);
              }
              if (value is Byte[]) {
                  return ToString((Byte[])value, encoding);
              }
              return value.ToString();
          }
  
          #endregion
  
          #region ToStringBuilder
  
          public static StringBuilder ToStringBuilder(Object value) {
              return ToStringBuilder(value, UTF8);
          }
  
          public static StringBuilder ToStringBuilder(Object value, String charset) {
              if (value == null) {
                  return null;
              }
              if (value is StringBuilder) {
                  return (StringBuilder)value;
              }
              if (value is String) {
                  return new StringBuilder((String)value);
              }
              if (value is Char[]) {
                  return new StringBuilder().Append((Char[])value);
              }
              if (value is Byte[]) {
                  return new StringBuilder(ToString((Byte[])value, charset));
              }
              return new StringBuilder().Append(value);
          }
  
          public static StringBuilder ToStringBuilder(Object value, Encoding encoding) {
              if (value == null) {
                  return null;
              }
              if (value is StringBuilder) {
                  return (StringBuilder)value;
              }
              if (value is String) {
                  return new StringBuilder((String)value);
              }
              if (value is Char[]) {
                  return new StringBuilder().Append((Char[])value);
              }
  
gt:             if (value is Byte[]) {