private static bool TryFromIntPtr( IntPtr value, XInfo from, XInfo to, out object result, string format) { result = GetDefaultValue(to.Category); switch (to.Category) { case TypeCategory.Array: return(TryToArray( from, to, value, out result, format )); case TypeCategory.IntPtr: result = value; return(true); case TypeCategory.Enum: case TypeCategory.Interface: case TypeCategory.Class: case TypeCategory.Struct: case TypeCategory.Stream: case TypeCategory.Color: case TypeCategory.String: case TypeCategory.DateTime: case TypeCategory.Decimal: case TypeCategory.Boolean: case TypeCategory.Char: case TypeCategory.Byte: case TypeCategory.SByte: case TypeCategory.Int16: case TypeCategory.UInt16: case TypeCategory.Int32: case TypeCategory.UInt32: case TypeCategory.UIntPtr: case TypeCategory.Int64: case TypeCategory.UInt64: case TypeCategory.Single: case TypeCategory.Double: case TypeCategory.Null: case TypeCategory.Others: default: return(false); } }
internal static XInfo Get(object value) { if (null == value) { throw new ArgumentNullException("value"); } var type = value is Type ? (Type)value : value.GetType(); XInfo info; if (!_Cache.TryGetValue(type, out info)) { info = new XInfo(type); _Cache.TryAdd(type, info); } return(info); }
private static bool TryFromArray( Array value, XInfo from, XInfo to, out object result, string format) { result = GetDefaultValue(to.Category); var bytes = from.IsBytes ? value.Cast <byte>().ToArray() : new byte[0]; switch (to.Category) { case TypeCategory.Array: return(TryToArray( from, to, value, out result, format )); case TypeCategory.DateTime: if (from.IsBytes) { if (bytes.Length == 8) { result = bytes.To <Int64>().To <DateTime>(); return(true); } else { var s = IsToConfig.Encoding.GetString(bytes); return(TryFromString( s, XInfo.String.Value, to, out result, format )); } } return(false); case TypeCategory.Enum: case TypeCategory.Class: case TypeCategory.Struct: case TypeCategory.Stream: case TypeCategory.Color: case TypeCategory.String: if (from.IsBytes) { var str = string.Empty; switch (format.ToUpper()) { case "DEFAULT": str = Encoding.Default.GetString(bytes); break; case "ASCII": str = Encoding.ASCII.GetString(bytes); break; case "UNICODE": str = Encoding.Unicode.GetString(bytes); break; case "UTF7": str = Encoding.UTF7.GetString(bytes); break; case "UTF8": str = Encoding.UTF8.GetString(bytes); break; case "UTF32": str = Encoding.UTF32.GetString(bytes); break; default: str = IsToConfig.Encoding.GetString(bytes); break; } return(TryFromString( str, XInfo.String.Value, to, out result, format )); } return(false); case TypeCategory.Byte: if (from.IsBytes) { if (bytes.Length == 0) { result = bytes[0]; return(true); } } return(false); case TypeCategory.SByte: if (from.IsBytes) { if (bytes.Length == 0) { result = (SByte)bytes[0]; return(NumericCompare(value, result)); } } return(false); case TypeCategory.Decimal: if (bytes.Length != 16) { // A decimal must be created from exactly // 128 bits / 16 bytes return(false); } var bits = new Int32[4]; for (int i = 0; i <= 15; i += 4) { bits[i / 4] = BitConverter.ToInt32(bytes, i); } result = new decimal(bits); return(true); case TypeCategory.Boolean: if (from.IsBytes) { result = BitConverter.ToBoolean(bytes, 0); return(true); } return(false); case TypeCategory.Char: if (from.IsBytes) { result = BitConverter.ToChar(bytes, 0); return(true); } return(false); case TypeCategory.Int16: if (from.IsBytes) { result = BitConverter.ToInt16(bytes, 0); return(true); } return(false); case TypeCategory.UInt16: if (from.IsBytes) { result = BitConverter.ToUInt16(bytes, 0); return(true); } return(false); case TypeCategory.Int32: if (from.IsBytes) { result = BitConverter.ToInt32(bytes, 0); return(true); } return(false); case TypeCategory.UInt32: if (from.IsBytes) { result = BitConverter.ToUInt32(bytes, 0); return(true); } return(false); case TypeCategory.Int64: if (from.IsBytes) { result = BitConverter.ToInt64(bytes, 0); return(true); } return(false); case TypeCategory.UInt64: if (from.IsBytes) { result = BitConverter.ToUInt64(bytes, 0); return(true); } return(false); case TypeCategory.Single: if (from.IsBytes) { result = BitConverter.ToSingle(bytes, 0); return(true); } return(false); case TypeCategory.Double: if (from.IsBytes) { result = BitConverter.ToDouble(bytes, 0); return(true); } return(false); case TypeCategory.Interface: case TypeCategory.IntPtr: case TypeCategory.UIntPtr: case TypeCategory.Null: case TypeCategory.Others: default: return(false); } }
private static bool TryFromSByte( sbyte value, XInfo from, XInfo to, out object result, string format) { result = GetDefaultValue(to.Category); switch (to.Category) { case TypeCategory.Array: return(TryToArray( from, to, value, out result, format )); case TypeCategory.SByte: result = value; return(true); case TypeCategory.Stream: var bytes = BitConverter.GetBytes(value); result = new MemoryStream(bytes); return(true); case TypeCategory.Enum: case TypeCategory.String: var s = value.ToString(); return(TryFromString( s, XInfo.String.Value, to, out result, format )); case TypeCategory.Char: result = Convert.ToChar(value); return(true); case TypeCategory.Boolean: result = value > 0; return(true); case TypeCategory.Color: var i = (Int32)value; return(TryFromInt32( i, XInfo.Int32.Value, to, out result, format )); case TypeCategory.Decimal: result = (Decimal)value; return(NumericCompare(value, result)); case TypeCategory.Byte: result = (Byte)value; return(NumericCompare(value, result)); case TypeCategory.Int16: result = (Int16)value; return(NumericCompare(value, result)); case TypeCategory.UInt16: result = (UInt16)value; return(NumericCompare(value, result)); case TypeCategory.Int32: result = (Int32)value; return(NumericCompare(value, result)); case TypeCategory.UInt32: result = (UInt32)value; return(NumericCompare(value, result)); case TypeCategory.Int64: result = (Int64)value; return(NumericCompare(value, result)); case TypeCategory.UInt64: result = (UInt64)value; return(NumericCompare(value, result)); case TypeCategory.Single: result = (Single)value; return(NumericCompare(value, result)); case TypeCategory.Double: result = (Double)value; return(NumericCompare(value, result)); case TypeCategory.Interface: case TypeCategory.Class: case TypeCategory.Struct: case TypeCategory.DateTime: case TypeCategory.IntPtr: case TypeCategory.UIntPtr: case TypeCategory.Null: case TypeCategory.Others: default: return(false); } }
private static bool TryFromInt64( long value, XInfo from, XInfo to, out object result, string format) { result = GetDefaultValue(to.Category); switch (to.Category) { case TypeCategory.Array: if (to.IsBytes) { result = BitConverter.GetBytes(value); return(true); } return(TryToArray( from, to, value, out result, format )); case TypeCategory.Int64: result = value; return(true); case TypeCategory.Stream: var bytes = BitConverter.GetBytes(value); result = new MemoryStream(bytes); return(true); case TypeCategory.Enum: case TypeCategory.String: var s = value.ToString(); return(TryFromString( s, XInfo.String.Value, to, out result, format )); case TypeCategory.Char: result = Convert.ToChar(value); return(true); case TypeCategory.Boolean: result = value > 0; return(true); case TypeCategory.Color: var i = (Int32)value; if (!NumericCompare(value, result)) { return(false); } return(TryFromInt32( i, XInfo.Int32.Value, to, out result, format )); case TypeCategory.DateTime: try { result = DateTime.FromBinary(value); return(true); } catch (Exception ex) { LogRecord .Create() //.Add("value", value) //.Add(ex) .Error(); return(false); } case TypeCategory.Decimal: result = (Decimal)value; return(NumericCompare(value, result)); case TypeCategory.Byte: checked { result = (Byte)value; } return(NumericCompare(value, result)); case TypeCategory.SByte: checked { result = (SByte)value; } return(NumericCompare(value, result)); case TypeCategory.Int16: checked { result = (Int16)value; } return(NumericCompare(value, result)); case TypeCategory.UInt16: checked { result = (UInt16)value; } return(NumericCompare(value, result)); case TypeCategory.Int32: checked { result = (Int32)value; } return(NumericCompare(value, result)); case TypeCategory.UInt32: checked { result = (UInt32)value; } return(NumericCompare(value, result)); case TypeCategory.UInt64: checked { result = (UInt64)value; } return(NumericCompare(value, result)); case TypeCategory.Single: result = (Single)value; return(NumericCompare(value, result)); case TypeCategory.Double: result = (Double)value; return(NumericCompare(value, result)); case TypeCategory.Interface: case TypeCategory.Class: case TypeCategory.Struct: case TypeCategory.IntPtr: case TypeCategory.UIntPtr: case TypeCategory.Null: case TypeCategory.Others: default: return(false); } }
private static bool TryFromStream( Stream value, XInfo from, XInfo to, out object result, string format) { result = GetDefaultValue(to.Category); switch (to.Category) { case TypeCategory.Array: return(TryToArray( from, to, value, out result, format )); case TypeCategory.Stream: result = value; return(true); case TypeCategory.Enum: case TypeCategory.Color: case TypeCategory.Class: case TypeCategory.Struct: case TypeCategory.String: case TypeCategory.DateTime: case TypeCategory.Decimal: case TypeCategory.Boolean: case TypeCategory.Char: case TypeCategory.Byte: case TypeCategory.SByte: case TypeCategory.Int16: case TypeCategory.UInt16: case TypeCategory.Int32: case TypeCategory.UInt32: case TypeCategory.Int64: case TypeCategory.UInt64: case TypeCategory.Single: case TypeCategory.Double: using (var memoryStream = new MemoryStream()) { value.CopyTo(memoryStream); var bytes = memoryStream.ToArray(); return(TryFromArray( bytes, ToCache.Get(typeof(byte[])), to, out result, format )); } case TypeCategory.Interface: case TypeCategory.IntPtr: case TypeCategory.UIntPtr: case TypeCategory.Null: case TypeCategory.Others: default: return(false); } }
private static bool TryFromEnum( object value, XInfo from, XInfo to, out object result, string format) { result = GetDefaultValue(to.Category); decimal decimalVal; var decimalFlag = decimal.TryParse( ((int)value).ToString(), out decimalVal ); switch (to.Category) { case TypeCategory.Array: return(TryToArray( from, to, value, out result, format )); case TypeCategory.Enum: if (from.Equals(to)) { result = value; return(true); } return(false); case TypeCategory.String: result = IsToConverter.FromEnum.ToString( from.Type, value ); return(true); case TypeCategory.Stream: return(TryFromString( value.ToString(), XInfo.String.Value, to, out result, format )); case TypeCategory.Char: result = Convert.ToChar((int)value); return(true); case TypeCategory.Decimal: result = decimalVal; return(decimalFlag); case TypeCategory.Byte: if (!decimalFlag) { return(false); } byte byteVal; if (decimalVal.TryTo <byte>(out byteVal)) { result = byteVal; return(true); } else { return(false); } case TypeCategory.SByte: if (!decimalFlag) { return(false); } sbyte sbyteVal; if (decimalVal.TryTo <sbyte>(out sbyteVal)) { result = sbyteVal; return(true); } else { return(false); } case TypeCategory.Int16: if (!decimalFlag) { return(false); } Int16 int16Val; if (decimalVal.TryTo <Int16>(out int16Val)) { result = int16Val; return(true); } else { return(false); } case TypeCategory.UInt16: if (!decimalFlag) { return(false); } UInt16 uint16Val; if (decimalVal.TryTo <UInt16>(out uint16Val)) { result = uint16Val; return(true); } else { return(false); } case TypeCategory.Int32: if (!decimalFlag) { return(false); } Int32 int32Val; if (decimalVal.TryTo <Int32>(out int32Val)) { result = int32Val; return(true); } else { return(false); } case TypeCategory.UInt32: if (!decimalFlag) { return(false); } UInt32 uint32Val; if (decimalVal.TryTo <UInt32>(out uint32Val)) { result = uint32Val; return(true); } else { return(false); } case TypeCategory.Int64: if (!decimalFlag) { return(false); } Int64 int64Val; if (decimalVal.TryTo <Int64>(out int64Val)) { result = int64Val; return(true); } else { return(false); } case TypeCategory.UInt64: if (!decimalFlag) { return(false); } UInt64 uint64Val; if (decimalVal.TryTo <UInt64>(out uint64Val)) { result = uint64Val; return(true); } else { return(false); } case TypeCategory.Single: if (!decimalFlag) { return(false); } Single singleVal; if (decimalVal.TryTo <Single>(out singleVal)) { result = singleVal; return(true); } else { return(false); } case TypeCategory.Double: if (!decimalFlag) { return(false); } Double doubleVal; if (decimalVal.TryTo <Double>(out doubleVal)) { result = doubleVal; return(true); } else { return(false); } case TypeCategory.IntPtr: case TypeCategory.UIntPtr: case TypeCategory.Interface: case TypeCategory.Class: case TypeCategory.Struct: case TypeCategory.Color: case TypeCategory.DateTime: case TypeCategory.Boolean: case TypeCategory.Null: case TypeCategory.Others: default: return(false); } }
private static bool TryFromString( string value, XInfo from, XInfo to, out object result, string format) { result = GetDefaultValue(to.Category); decimal decimalVal; var decimalFlag = decimal.TryParse(value, out decimalVal); switch (to.Category) { case TypeCategory.Array: if (to.IsBytes) { result = IsToConfig.Encoding.GetBytes(value); return(true); } var firstElement = to .ElementInfos .FirstOrDefault(); if ( null != firstElement && firstElement.Category == TypeCategory.Class) { if (!IsJson(value)) { return(false); } result = JsonConvert.DeserializeObject( value, to.Type ); return(true); } return(TryToArray( from, to, value, out result, format )); case TypeCategory.String: result = value; return(true); case TypeCategory.Stream: result = new MemoryStream(value.To <byte[]>()); return(true); case TypeCategory.Enum: result = IsToConverter.FromString.ToEnum( to.Type, value ); return(true); case TypeCategory.Class: case TypeCategory.Struct: if (IsJson(value)) { result = JsonConvert.DeserializeObject( value, to.Type ); return(true); } return(false); case TypeCategory.DateTime: DateTime val; if (DateTime.TryParse(value, out val)) { result = val; return(true); } else { int[] points; if (TryParseDate(value, out points)) { try { var millisecond = points[6] .To <string>() .Left(3) .To <int>(); result = new DateTime( ToExtender.Enclosed(points[0], 1, 9999), ToExtender.Enclosed(points[1], 1, 12), ToExtender.Enclosed(points[2], 1, 31), ToExtender.Enclosed(points[3], 0, 23), ToExtender.Enclosed(points[4], 0, 59), ToExtender.Enclosed(points[5], 0, 59), millisecond ); } catch (Exception ex) { LogRecord .Create() //.Add("Value", value) //.Add("From", from.FullName) //.Add("To", to.FullName) //.Add( // "Points", // points // .Select(x => x.ToString()) // .Join(Symbol.CommaSpace) //) //.Add(ex) .Error(); return(false); } return(true); } else { return(false); } } case TypeCategory.Boolean: if (IsNumeric(value)) { var i = value.To <int>(); result = i > 0; return(true); } foreach (var one in IsToConfig.TrueStringArray) { if (one.Equals( value, StringComparison.OrdinalIgnoreCase)) { result = true; break; } } return(true); case TypeCategory.Char: result = Convert.ToChar(value); return(true); case TypeCategory.Decimal: result = decimalVal; return(decimalFlag); case TypeCategory.Byte: if (!decimalFlag) { return(false); } byte byteVal; if (decimalVal.TryTo <byte>(out byteVal)) { result = byteVal; return(true); } else { return(false); } case TypeCategory.SByte: if (!decimalFlag) { return(false); } sbyte sbyteVal; if (decimalVal.TryTo <sbyte>(out sbyteVal)) { result = sbyteVal; return(true); } else { return(false); } case TypeCategory.Int16: if (!decimalFlag) { return(false); } Int16 int16Val; if (decimalVal.TryTo <Int16>(out int16Val)) { result = int16Val; return(true); } else { return(false); } case TypeCategory.UInt16: if (!decimalFlag) { return(false); } UInt16 uint16Val; if (decimalVal.TryTo <UInt16>(out uint16Val)) { result = uint16Val; return(true); } else { return(false); } case TypeCategory.Int32: if (!decimalFlag) { return(false); } Int32 int32Val; if (decimalVal.TryTo <Int32>(out int32Val)) { result = int32Val; return(true); } else { return(false); } case TypeCategory.UInt32: if (!decimalFlag) { return(false); } UInt32 uint32Val; if (decimalVal.TryTo <UInt32>(out uint32Val)) { result = uint32Val; return(true); } else { return(false); } case TypeCategory.Int64: if (!decimalFlag) { return(false); } Int64 int64Val; if (decimalVal.TryTo <Int64>(out int64Val)) { result = int64Val; return(true); } else { return(false); } case TypeCategory.UInt64: if (!decimalFlag) { return(false); } UInt64 uint64Val; if (decimalVal.TryTo <UInt64>(out uint64Val)) { result = uint64Val; return(true); } else { return(false); } case TypeCategory.Single: if (!decimalFlag) { return(false); } Single singleVal; if (decimalVal.TryTo <Single>(out singleVal)) { result = singleVal; return(true); } else { return(false); } case TypeCategory.Double: if (!decimalFlag) { return(false); } Double doubleVal; if (decimalVal.TryTo <Double>(out doubleVal)) { result = doubleVal; return(true); } else { return(false); } case TypeCategory.Interface: case TypeCategory.IntPtr: case TypeCategory.UIntPtr: case TypeCategory.Null: case TypeCategory.Others: default: return(false); } }
private static bool TryFromClass( object value, XInfo from, XInfo to, out object result, string format) { result = GetDefaultValue(to.Category); switch (to.Category) { case TypeCategory.Array: return(TryToArray( from, to, value, out result, format )); case TypeCategory.Class: if (from.Equals(to)) { result = value; return(true); } var underlyingType = Nullable.GetUnderlyingType( to.Type ); var converter = underlyingType == null ? TypeDescriptor.GetConverter(to.Type) : TypeDescriptor.GetConverter(underlyingType); if (null != converter) { try { result = converter.ConvertFrom(value); return(true); } catch (Exception ex) { LogRecord .Create() //.Add("Typeconverter", converter.GetType().Name) //.Add("From", from?.Type.Name) //.Add("To", to?.Type.Name) //.Add("ValueType", value?.GetType().Name) //.Add("Value", value) //.Add(ex) .Error(); } } result = ForceClone(value, to.Type); return(true); case TypeCategory.Struct: var dic = GetValues(value); result = SetValues(dic, to.Type); return(true); case TypeCategory.Enum: case TypeCategory.Interface: case TypeCategory.Stream: case TypeCategory.Color: case TypeCategory.String: case TypeCategory.DateTime: case TypeCategory.Decimal: case TypeCategory.Boolean: case TypeCategory.Char: case TypeCategory.Byte: case TypeCategory.SByte: case TypeCategory.Int16: case TypeCategory.UInt16: case TypeCategory.Int32: case TypeCategory.UInt32: case TypeCategory.IntPtr: case TypeCategory.UIntPtr: case TypeCategory.Int64: case TypeCategory.UInt64: case TypeCategory.Single: case TypeCategory.Double: case TypeCategory.Null: case TypeCategory.Others: default: return(false); } }
private static bool TryFromByte( byte value, XInfo from, XInfo to, out object result, string format) { result = GetDefaultValue(to.Category); switch (to.Category) { case TypeCategory.Array: return(TryToArray( from, to, value, out result, format )); case TypeCategory.Byte: result = value; return(true); case TypeCategory.Stream: var bytes = BitConverter.GetBytes(value); result = new MemoryStream(bytes); return(true); case TypeCategory.String: var input = IsToConfig.Encoding.GetString( new[] { value } ); return(TryFromString( input, XInfo.String.Value, to, out result, format )); case TypeCategory.Enum: result = Enum.Parse(to.Type, value.To <string>()); return(true); case TypeCategory.Char: result = Convert.ToChar(value); return(true); case TypeCategory.Boolean: result = value > 0; return(true); case TypeCategory.Decimal: result = (Decimal)value; return(true); case TypeCategory.SByte: checked { result = (SByte)value; } return(true); case TypeCategory.Int16: result = (Int16)value; return(true); case TypeCategory.UInt16: checked { result = (UInt16)value; } return(true); case TypeCategory.Int32: result = (Int32)value; return(true); case TypeCategory.UInt32: checked { result = (UInt32)value; } return(true); case TypeCategory.Int64: result = (Int64)value; return(true); case TypeCategory.UInt64: checked { result = (UInt64)value; } return(true); case TypeCategory.Single: result = (Single)value; return(true); case TypeCategory.Double: result = (Double)value; return(true); //case TypeCategory.Color: // result = Color.FromArgb((Int32)value); // return true; case TypeCategory.Interface: case TypeCategory.Class: case TypeCategory.Struct: case TypeCategory.DateTime: case TypeCategory.IntPtr: case TypeCategory.UIntPtr: case TypeCategory.Null: case TypeCategory.Others: default: return(false); } }
private static bool TryFromColor( Color value, XInfo from, XInfo to, out object result, string format) { result = GetDefaultValue(to.Category); var i = value.ToArgb(); var n = value.Name; switch (to.Category) { case TypeCategory.Array: return(TryToArray( from, to, value, out result, format )); case TypeCategory.Color: result = value; return(true); case TypeCategory.Enum: try { result = Enum.Parse(to.Type, n); return(true); } catch { } try { result = Enum.Parse(to.Type, i.ToString()); return(true); } catch { } return(false); case TypeCategory.Stream: case TypeCategory.String: var f = string.IsNullOrEmpty(format) ? "html" : format.ToLower(); var s = string.Empty; switch (f) { case "html": s = ColorTranslator.ToHtml(value); break; case "rgb": s = string.Format( "rgb({0}, {1}, {2})", value.R, value.G, value.B ); break; case "argb": s = string.Format( "rgba({0}, {1}, {2}, {3}%)", value.R, value.G, value.B, value.A ); break; case "name": s = value.Name; break; default: return(false); } return(TryFromString( s, XInfo.String.Value, to, out result, format )); case TypeCategory.Decimal: result = (Decimal)i; return(NumericCompare(i, result)); case TypeCategory.Byte: checked { result = (Byte)i; } return(NumericCompare(i, result)); case TypeCategory.SByte: checked { result = (SByte)i; } return(NumericCompare(i, result)); case TypeCategory.Int16: checked { result = (Int16)i; } return(NumericCompare(i, result)); case TypeCategory.UInt16: checked { result = (UInt16)i; } return(NumericCompare(i, result)); case TypeCategory.Int32: result = (Int32)i; return(NumericCompare(i, result)); case TypeCategory.UInt32: checked { result = (UInt32)i; } return(NumericCompare(i, result)); case TypeCategory.Int64: result = (Int64)i; return(NumericCompare(i, result)); case TypeCategory.UInt64: result = (UInt64)i; return(NumericCompare(i, result)); case TypeCategory.Single: result = (Single)i; return(NumericCompare(i, result)); case TypeCategory.Double: result = (Double)i; return(NumericCompare(i, result)); case TypeCategory.DateTime: case TypeCategory.Boolean: case TypeCategory.Char: case TypeCategory.Class: case TypeCategory.Struct: case TypeCategory.Interface: case TypeCategory.IntPtr: case TypeCategory.UIntPtr: case TypeCategory.Null: case TypeCategory.Others: default: return(false); } }
private static bool TryToArray( XInfo from, XInfo to, object value, out object result, string format = "") { result = null; try { var elementType = to .ElementInfos .FirstOrDefault() .Type; var genericListTypeRunTime = typeof(List <>) .MakeGenericType(new[] { elementType }); var genericList = Activator.CreateInstance( genericListTypeRunTime ); var genericListType = genericList.GetType(); // Add var addInfo = genericListType .GetMethods() .FirstOrDefault(x => x.Name == "Add"); if (from.Category == TypeCategory.Array) { if (to.ElementInfos.Count() == 1) { var datas = value as IEnumerable; if (null != datas) { var en = datas.GetEnumerator(); while (en.MoveNext()) { object val; if (TryTo( en.Current, elementType, out val)) { addInfo.Invoke( genericList, new object[] { val } ); } else { return(false); } } } } else { // } } else { if (to.ElementInfos.Count() == 1) { object val; if (value.Is(elementType)) { addInfo.Invoke( genericList, new object[] { value } ); } else { if (TryTo(value, elementType, out val)) { addInfo.Invoke( genericList, new object[] { val } ); } else { return(false); } } } else { // } } if (to.Type.FullName == genericListType.FullName) { // List result = genericList; } else if (to.Type.Is <IQueryable>()) { // IQueryable result = ((IList)genericList).AsQueryable(); } else if ( to.Category == TypeCategory.Interface && to.Type.Is <IList>()) { // IList result = (IList)genericList; } else { // Array var toarrayInfo = genericListType .GetMethods() .FirstOrDefault(x => x.Name == "ToArray"); if (null == toarrayInfo) { return(false); } result = toarrayInfo.Invoke( genericList, new object[] { } ); } return(true); } catch (Exception ex) { LogRecord .Create() //.Add("From", from?.Type.Name) //.Add("To", to?.Type.Name) //.Add("ValueType", value?.GetType().Name) //.Add("Value", value) //.Add(ex) .Error(); return(false); } }
private static bool TryFromChar( char value, XInfo from, XInfo to, out object result, string format) { result = GetDefaultValue(to.Category); var d = (int)value; var isNumeric = true; var s = value.ToString(); switch (to.Category) { case TypeCategory.Array: if (to.IsBytes) { result = BitConverter.GetBytes(value); return(true); } return(TryToArray( from, to, value, out result, format )); case TypeCategory.Char: result = value; return(true); case TypeCategory.Stream: var bytes = BitConverter.GetBytes(value); result = new MemoryStream(bytes); return(true); case TypeCategory.Enum: case TypeCategory.Class: case TypeCategory.Struct: case TypeCategory.Color: case TypeCategory.String: case TypeCategory.DateTime: return(TryFromString( s, XInfo.String.Value, to, out result, format )); case TypeCategory.Boolean: if (isNumeric) { result = d > 0; return(true); } return(false); case TypeCategory.Decimal: if (isNumeric) { result = (Decimal)d; return(NumericCompare(d, result)); } return(false); case TypeCategory.Byte: if (isNumeric) { checked { result = (Byte)d; } return(NumericCompare(d, result)); } return(false); case TypeCategory.SByte: if (isNumeric) { checked { result = (SByte)d; } return(NumericCompare(d, result)); } return(false); case TypeCategory.Int16: if (isNumeric) { checked { result = (Int16)d; } return(NumericCompare(d, result)); } return(false); case TypeCategory.UInt16: if (isNumeric) { checked { result = (UInt16)d; } return(NumericCompare(d, result)); } return(false); case TypeCategory.Int32: if (isNumeric) { result = (Int32)d; return(NumericCompare(d, result)); } return(false); case TypeCategory.UInt32: if (isNumeric) { checked { result = (UInt32)d; } return(NumericCompare(d, result)); } return(false); case TypeCategory.Int64: if (isNumeric) { result = (Int64)d; return(NumericCompare(d, result)); } return(false); case TypeCategory.UInt64: if (isNumeric) { result = (UInt64)d; return(NumericCompare(d, result)); } return(false); case TypeCategory.Single: if (isNumeric) { result = (Single)d; return(NumericCompare(d, result)); } return(false); case TypeCategory.Double: if (isNumeric) { result = (Double)d; return(NumericCompare(d, result)); } return(false); case TypeCategory.Interface: case TypeCategory.IntPtr: case TypeCategory.UIntPtr: case TypeCategory.Null: case TypeCategory.Others: default: return(false); } }
private static bool TryFromDateTime( DateTime value, XInfo from, XInfo to, out object result, string format) { result = GetDefaultValue(to.Category); switch (to.Category) { case TypeCategory.Array: if (to.IsBytes) { result = value.ToBinary().To <byte[]>(); return(true); } return(TryToArray( from, to, value, out result, format )); case TypeCategory.DateTime: result = value; return(true); case TypeCategory.String: case TypeCategory.Stream: var s = value.ToString( string.IsNullOrEmpty(format) ? "yyyy-MM-dd HH:mm:ss.fffffff" : format ); return(TryFromString( s, XInfo.String.Value, to, out result, format )); case TypeCategory.Char: result = Convert.ToChar(value); return(true); case TypeCategory.Int64: result = value.ToBinary(); return(true); case TypeCategory.Decimal: case TypeCategory.Byte: case TypeCategory.SByte: case TypeCategory.Int16: case TypeCategory.UInt16: case TypeCategory.Int32: case TypeCategory.UInt32: case TypeCategory.UInt64: case TypeCategory.Single: case TypeCategory.Double: return(false); case TypeCategory.Boolean: case TypeCategory.Enum: case TypeCategory.Color: case TypeCategory.Interface: case TypeCategory.Class: case TypeCategory.Struct: case TypeCategory.IntPtr: case TypeCategory.UIntPtr: case TypeCategory.Null: case TypeCategory.Others: default: return(false); } }