private void Init(Type type) { if (null == type) { IsNull = true; Category = TypeCategory.Null; return; } IsNull = false; Type = type; FullName = type.FullName; IsValueType = type.IsValueType; IsPrimitive = type.IsPrimitive; ElementInfos = new List <XInfo>(); switch (FullName) { case "System.String": Category = TypeCategory.String; break; case "System.DateTime": Category = TypeCategory.DateTime; break; case "System.Decimal": Category = TypeCategory.Decimal; break; case "System.Boolean": Category = TypeCategory.Boolean; break; case "System.Char": Category = TypeCategory.Char; break; case "System.Byte": Category = TypeCategory.Byte; break; case "System.SByte": Category = TypeCategory.SByte; break; case "System.Int16": Category = TypeCategory.Int16; break; case "System.UInt16": Category = TypeCategory.UInt16; break; case "System.Int32": Category = TypeCategory.Int32; break; case "System.UInt32": Category = TypeCategory.UInt32; break; case "System.IntPtr": Category = TypeCategory.IntPtr; break; case "stem.UIntPtr": Category = TypeCategory.UIntPtr; break; case "System.Int64": Category = TypeCategory.Int64; break; case "System.UInt64": Category = TypeCategory.UInt64; break; case "System.Single": Category = TypeCategory.Single; break; case "System.Double": Category = TypeCategory.Double; break; case "System.IO.Stream": Category = TypeCategory.Stream; break; case "System.Drawing.Color": Category = TypeCategory.Color; break; //case "Array": //case "Enum": //case "Interface": //case "Class": //case "Struct": //case "Others": default: if (type.IsArray || type.Is <IEnumerable>()) { Category = TypeCategory.Array; break; } if (type.IsEnum) { Category = TypeCategory.Enum; break; } if (type.IsInterface) { Category = TypeCategory.Interface; break; } if (type.IsClass) { this.Category = TypeCategory.Class; break; } if ( type.IsValueType && !type.IsPrimitive && !type.IsClass && !type.IsEnum ) { this.Category = TypeCategory.Struct; break; } this.Category = TypeCategory.Others; break; } if (Category == TypeCategory.Array) { var elementType = type.GetElementType(); if (null == elementType) { var genericTypes = type.GenericTypeArguments; if (null != genericTypes) { foreach (var genericType in genericTypes) { var info = ToCache.Get(genericType); ElementInfos.Add(info); IsBytes = info.Category == TypeCategory.Byte; } } else { // } } else { var info = ToCache.Get(elementType); ElementInfos.Add(info); IsBytes = info.Category == TypeCategory.Byte; } } }
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 TryTo( object value, Type type, out object result, string format = "") { result = string.Empty; if (null == value || null == type) { return(false); } var from = ToCache.Get(value); var interceptor = ToInterceptor.Get(from.Type); if (null != interceptor) { return(interceptor.TryTo( value, type, out result, format )); } var to = ToCache.Get(type); try { switch (from.Category) { case TypeCategory.Array: object arry = value as Array; if (null == arry) { var toarrayInfo = from .Type .GetMethods() .FirstOrDefault(x => x.Name == "ToArray" ); if (null == toarrayInfo) { return(false); } arry = toarrayInfo.Invoke( value, new object[] { } ); } if (null == arry) { return(false); } return(TryFromArray( arry as Array, from, to, out result, format )); case TypeCategory.Enum: return(TryFromEnum( value, from, to, out result, format )); case TypeCategory.Interface: return(TryFromInterface( value, from, to, out result, format )); case TypeCategory.Class: return(TryFromClass( value, from, to, out result, format )); case TypeCategory.Stream: return(TryFromStream( value as Stream, from, to, out result, format )); case TypeCategory.Color: //return TryFromColor( // (Color)value, // from, // to, // out result, // format //); case TypeCategory.String: return(TryFromString( value as string, from, to, out result, format )); case TypeCategory.DateTime: return(TryFromDateTime( (DateTime)value, from, to, out result, format )); case TypeCategory.Decimal: return(TryFromDecimal( (decimal)value, from, to, out result, format )); case TypeCategory.Boolean: return(TryFromBoolean( (bool)value, from, to, out result, format )); case TypeCategory.Char: return(TryFromChar( (char)value, from, to, out result, format )); case TypeCategory.Byte: return(TryFromByte( (byte)value, from, to, out result, format )); case TypeCategory.SByte: return(TryFromSByte( (sbyte)value, from, to, out result, format )); case TypeCategory.Int16: return(TryFromInt16( (short)value, from, to, out result, format )); case TypeCategory.UInt16: return(TryFromUInt16( (ushort)value, from, to, out result, format )); case TypeCategory.Int32: return(TryFromInt32( (int)value, from, to, out result, format )); case TypeCategory.UInt32: return(TryFromUInt32( (uint)value, from, to, out result, format )); case TypeCategory.IntPtr: return(TryFromIntPtr( (IntPtr)value, from, to, out result, format )); case TypeCategory.UIntPtr: return(TryFromUIntPtr( (UIntPtr)value, from, to, out result, format )); case TypeCategory.Int64: return(TryFromInt64( (long)value, from, to, out result, format )); case TypeCategory.UInt64: return(TryFromUInt64( (ulong)value, from, to, out result, format )); case TypeCategory.Single: return(TryFromSingle( (float)value, from, to, out result, format )); case TypeCategory.Double: return(TryFromDouble( (double)value, from, to, out result, format )); case TypeCategory.Struct: return(TryFromStruct( value, from, to, out result, format )); case TypeCategory.Null: return(false); case TypeCategory.Others: return(false); default: return(false); } } catch (Exception ex) { LogRecord .Create() //.Add("Value", value?.ToString()) //.Add("Type", type?.GetType().Name) //.Add("Result", result) //.Add("Format", format) //.Add(ex) .Error(); return(false); } }