예제 #1
0
 public static object FromUniversalString(string strValue, Type type)
 {
     if (strValue == c_strConstNull)
     {
         return(null);
     }
     try
     {
         if (type == typeof(SByte) || type == typeof(SByte?))
         {
             return(SByte.Parse(strValue, CultureInfo.CurrentCulture));
         }
         else if (type == typeof(Int16) || type == typeof(Int16?))
         {
             return(Int16.Parse(strValue, CultureInfo.CurrentCulture));
         }
         else if (type == typeof(Int32) || type == typeof(Int32?))
         {
             return(Int32.Parse(strValue, CultureInfo.CurrentCulture));
         }
         else if (type == typeof(Int64) || type == typeof(Int64?))
         {
             return(Int64.Parse(strValue, CultureInfo.CurrentCulture));
         }
         else if (type == typeof(Byte) || type == typeof(Byte?))
         {
             return(Byte.Parse(strValue, CultureInfo.CurrentCulture));
         }
         else if (type == typeof(UInt16) || type == typeof(UInt16?))
         {
             return(UInt16.Parse(strValue, CultureInfo.CurrentCulture));
         }
         else if (type == typeof(UInt32) || type == typeof(UInt32?))
         {
             return(UInt32.Parse(strValue, CultureInfo.CurrentCulture));
         }
         else if (type == typeof(UInt64) || type == typeof(UInt64?))
         {
             return(UInt64.Parse(strValue, CultureInfo.CurrentCulture));
         }
         else if (type == typeof(Single) || type == typeof(Single?))
         {
             try
             {
                 return(Single.Parse(strValue, CultureInfo.CurrentCulture));
             }
             catch
             {
                 if (strValue.IndexOf('.') >= 0)
                 {
                     strValue = strValue.Replace('.', ',');
                 }
                 else
                 {
                     strValue = strValue.Replace(',', '.');
                 }
                 return(Single.Parse(strValue, CultureInfo.CurrentCulture));
             }
         }
         else if (type == typeof(Double) || type == typeof(Double?))
         {
             return(CUtilDouble.DoubleFromString(strValue));
         }
         else if (type == typeof(Decimal) || type == typeof(Decimal?))
         {
             try
             {
                 return(Decimal.Parse(strValue, CultureInfo.CurrentCulture));
             }
             catch
             {
                 if (strValue.IndexOf('.') >= 0)
                 {
                     strValue = strValue.Replace('.', ',');
                 }
                 else
                 {
                     strValue = strValue.Replace(',', '.');
                 }
                 return(Decimal.Parse(strValue, CultureInfo.CurrentCulture));
             }
         }
         else if (type == typeof(Boolean) || type == typeof(Boolean?))
         {
             return(strValue == "1");
         }
         else if (type == typeof(Char) || type == typeof(Char?))
         {
             if (strValue.Length > 0)
             {
                 return(strValue[0]);
             }
             return('\0');
         }
         else if (type == typeof(String))
         {
             if (strValue == "@" + c_strConstNull)
             {
                 return(c_strConstNull);
             }
             return(strValue);
         }
         else if (type == typeof(DateTime) || type == typeof(DateTime?))
         {
             return(CUtilDate.FromUniversalString(strValue));
         }
         else if (type == typeof(CDateTimeEx))
         {
             return(new CDateTimeEx(CUtilDate.FromUniversalString(strValue)));
         }
     }
     catch
     {
         return(null);
     }
     throw new Exception(I.T("Unknown type for ToUniversalString |30107") + type.ToString());
 }
예제 #2
0
 public static string ToUniversalString(object value)
 {
     if (value == null || value == DBNull.Value)
     {
         return(c_strConstNull);
     }
     if (value is SByte)
     {
         return(value.ToString());
     }
     else if (value is Int16)
     {
         return(value.ToString());
     }
     else if (value is Int32)
     {
         return(value.ToString());
     }
     else if (value is Int64)
     {
         return(value.ToString());
     }
     else if (value is Byte)
     {
         return(value.ToString());
     }
     else if (value is UInt16)
     {
         return(value.ToString());
     }
     else if (value is UInt32)
     {
         return(value.ToString());
     }
     else if (value is UInt64)
     {
         return(value.ToString());
     }
     else if (value is Single)
     {
         return(value.ToString().Replace(',', '.'));
     }
     else if (value is Double)
     {
         return(value.ToString().Replace(',', '.'));
     }
     else if (value is Decimal)
     {
         return(value.ToString().Replace(',', '.'));
     }
     else if (value is Boolean)
     {
         return(((bool)value)?"1":"0");
     }
     else if (value is Char)
     {
         return(value.ToString());
     }
     else if (value is String)
     {
         if (value.ToString() == c_strConstNull)
         {
             return("@" + c_strConstNull);
         }
         return(value.ToString());
     }
     else if (value is DateTime)
     {
         return(CUtilDate.GetUniversalString((DateTime)value));
     }
     else
     {
         CDateTimeEx dtex = (value as CDateTimeEx);
         if (dtex != null)
         {
             return(CUtilDate.GetUniversalString(dtex.DateTimeValue));
         }
     }
     throw new Exception(I.T("Unknown type for ToUniversalString |30107") + value.GetType().ToString());
 }