public static bool TryParse(string data, DataType flag, out SimpleType value) { value = Null; try { var numStyle = Insensitive.StartsWith(data.Trim(), "0x") ? NumberStyles.HexNumber : NumberStyles.Any; if (numStyle == NumberStyles.HexNumber) { data = data.Substring(data.IndexOf("0x", StringComparison.OrdinalIgnoreCase) + 2); } switch (flag) { case DataType.Bool: value = new SimpleType(Boolean.Parse(data)); return(true); case DataType.Char: value = new SimpleType(Char.Parse(data)); return(true); case DataType.Byte: value = new SimpleType(Byte.Parse(data, numStyle)); return(true); case DataType.SByte: value = new SimpleType(SByte.Parse(data, numStyle)); return(true); case DataType.Short: value = new SimpleType(Int16.Parse(data, numStyle)); return(true); case DataType.UShort: value = new SimpleType(UInt16.Parse(data, numStyle)); return(true); case DataType.Int: value = new SimpleType(Int32.Parse(data, numStyle)); return(true); case DataType.UInt: value = new SimpleType(UInt32.Parse(data, numStyle)); return(true); case DataType.Long: value = new SimpleType(Int64.Parse(data, numStyle)); return(true); case DataType.ULong: value = new SimpleType(UInt64.Parse(data, numStyle)); return(true); case DataType.Float: value = new SimpleType(Single.Parse(data, numStyle)); return(true); case DataType.Decimal: value = new SimpleType(Decimal.Parse(data, numStyle)); return(true); case DataType.Double: value = new SimpleType(Double.Parse(data, numStyle)); return(true); case DataType.String: value = new SimpleType(data); return(true); case DataType.DateTime: value = new SimpleType(DateTime.Parse(data, CultureInfo.CurrentCulture, DateTimeStyles.AllowWhiteSpaces)); return(true); case DataType.TimeSpan: value = new SimpleType(TimeSpan.Parse(data)); return(true); } } catch { } return(false); }
public static bool TryConvert(string data, DataType flag, out object val) { val = null; if (flag == DataType.Null) { return(false); } try { var numStyle = Insensitive.StartsWith(data.Trim(), "0x") ? NumberStyles.HexNumber : NumberStyles.Any; if (numStyle == NumberStyles.HexNumber) { data = data.Substring(data.IndexOf("0x", StringComparison.OrdinalIgnoreCase) + 2); } switch (flag) { case DataType.Bool: val = Boolean.Parse(data); return(true); case DataType.Char: val = Char.Parse(data); return(true); case DataType.Byte: val = Byte.Parse(data, numStyle); return(true); case DataType.SByte: val = SByte.Parse(data, numStyle); return(true); case DataType.Short: val = Int16.Parse(data, numStyle); return(true); case DataType.UShort: val = UInt16.Parse(data, numStyle); return(true); case DataType.Int: val = Int32.Parse(data, numStyle); return(true); case DataType.UInt: val = UInt32.Parse(data, numStyle); return(true); case DataType.Long: val = Int64.Parse(data, numStyle); return(true); case DataType.ULong: val = UInt64.Parse(data, numStyle); return(true); case DataType.Float: val = Single.Parse(data, numStyle); return(true); case DataType.Decimal: val = Decimal.Parse(data, numStyle); return(true); case DataType.Double: val = Double.Parse(data, numStyle); return(true); case DataType.String: val = data; return(true); case DataType.DateTime: val = DateTime.Parse(data, CultureInfo.CurrentCulture, DateTimeStyles.AllowWhiteSpaces); return(true); case DataType.TimeSpan: val = TimeSpan.Parse(data); return(true); default: return(false); } } catch { return(false); } }
public static bool TryParse <T>(string data, out T value) { value = default(T); try { object val = null; var numStyle = Insensitive.StartsWith(data.Trim(), "0x") ? NumberStyles.HexNumber : NumberStyles.Any; if (numStyle == NumberStyles.HexNumber) { data = data.Substring(data.IndexOf("0x", StringComparison.OrdinalIgnoreCase) + 2); } var f = DataTypes.Lookup(value); switch (f) { case DataType.Bool: val = Boolean.Parse(data); break; case DataType.Char: val = Char.Parse(data); break; case DataType.Byte: val = Byte.Parse(data, numStyle); break; case DataType.SByte: val = SByte.Parse(data, numStyle); break; case DataType.Short: val = Int16.Parse(data, numStyle); break; case DataType.UShort: val = UInt16.Parse(data, numStyle); break; case DataType.Int: val = Int32.Parse(data, numStyle); break; case DataType.UInt: val = UInt32.Parse(data, numStyle); break; case DataType.Long: val = Int64.Parse(data, numStyle); break; case DataType.ULong: val = UInt64.Parse(data, numStyle); break; case DataType.Float: val = Single.Parse(data, numStyle); break; case DataType.Decimal: val = Decimal.Parse(data, numStyle); break; case DataType.Double: val = Double.Parse(data, numStyle); break; case DataType.String: val = data; break; case DataType.DateTime: val = DateTime.Parse(data, CultureInfo.CurrentCulture, DateTimeStyles.AllowWhiteSpaces); break; case DataType.TimeSpan: val = TimeSpan.Parse(data); break; } value = (T)val; return(true); } catch { } return(false); }
public static bool TryParse(string data, DataType flag, out SimpleType value) { try { switch (flag) { case DataType.Null: value = new SimpleType(null); break; case DataType.Bool: value = new SimpleType(Boolean.Parse(data)); break; case DataType.Char: value = new SimpleType(Char.Parse(data)); break; case DataType.Byte: value = new SimpleType(Byte.Parse(data)); break; case DataType.SByte: value = new SimpleType(SByte.Parse(data)); break; case DataType.Short: value = new SimpleType(Int16.Parse(data)); break; case DataType.UShort: value = new SimpleType(UInt16.Parse(data)); break; case DataType.Int: value = new SimpleType(Int32.Parse(data)); break; case DataType.UInt: value = new SimpleType(UInt32.Parse(data)); break; case DataType.Long: value = new SimpleType(Int64.Parse(data)); break; case DataType.ULong: value = new SimpleType(UInt64.Parse(data)); break; case DataType.Float: value = new SimpleType(Single.Parse(data)); break; case DataType.Decimal: value = new SimpleType(Decimal.Parse(data)); break; case DataType.Double: value = new SimpleType(Double.Parse(data)); break; case DataType.String: value = new SimpleType(data); break; case DataType.DateTime: value = new SimpleType(DateTime.Parse(data)); break; case DataType.TimeSpan: value = new SimpleType(TimeSpan.Parse(data)); break; default: value = new SimpleType(null); break; } return(true); } catch { value = new SimpleType(null); return(false); } }