// Token: 0x060016B8 RID: 5816 RVA: 0x0006B364 File Offset: 0x00069564 public static object ConvertOrCast(object initialValue, CultureInfo culture, Type targetType) { if (targetType == typeof(object)) { return(initialValue); } if (initialValue == null && Class_514.IsNullable(targetType)) { return(null); } object result; if (Class_499.TryConvert(initialValue, culture, targetType, out result)) { return(result); } return(Class_499.EnsureTypeAssignable(initialValue, Class_514.GetObjectType(initialValue), targetType)); }
// Token: 0x060016B9 RID: 5817 RVA: 0x0006B3BC File Offset: 0x000695BC private static object EnsureTypeAssignable(object value, Type initialType, Type targetType) { Type type = (value != null) ? value.GetType() : null; if (value != null) { if (targetType.IsAssignableFrom(type)) { return(value); } Func <object, object> func = Class_499.CastConverters.Get(new Class_499.TypeConvertKey(type, targetType)); if (func != null) { return(func(value)); } } else if (Class_514.IsNullable(targetType)) { return(null); } throw new ArgumentException("Could not cast or convert from {0} to {1}.".FormatWith(CultureInfo.InvariantCulture, (initialType != null) ? initialType.ToString() : "{null}", targetType)); }
// Token: 0x06001792 RID: 6034 RVA: 0x0006FC5C File Offset: 0x0006DE5C public static object GetDefaultValue(Type type) { if (!type.IsValueType) { return(null); } Class_496 typeCode = Class_499.GetTypeCode(type); switch (typeCode) { case Class_496.Char: case Class_496.SByte: case Class_496.Int16: case Class_496.UInt16: case Class_496.Int32: case Class_496.Byte: case Class_496.UInt32: return(0); case Class_496.CharNullable: case Class_496.BooleanNullable: case Class_496.SByteNullable: case Class_496.Int16Nullable: case Class_496.UInt16Nullable: case Class_496.Int32Nullable: case Class_496.ByteNullable: case Class_496.UInt32Nullable: case Class_496.Int64Nullable: case Class_496.UInt64Nullable: case Class_496.SingleNullable: case Class_496.DoubleNullable: case Class_496.DateTimeNullable: case Class_496.DateTimeOffsetNullable: case Class_496.DecimalNullable: break; case Class_496.Boolean: return(false); case Class_496.Int64: case Class_496.UInt64: return(0L); case Class_496.Single: return(0f); case Class_496.Double: return(0.0); case Class_496.DateTime: return(default(DateTime)); case Class_496.DateTimeOffset: return(default(DateTimeOffset)); case Class_496.Decimal: return(0m); case Class_496.Guid: return(default(Guid)); default: if (typeCode == Class_496.BigInteger) { return(default(BigInteger)); } break; } if (Class_514.IsNullable(type)) { return(null); } return(Activator.CreateInstance(type)); }
// Token: 0x060016B7 RID: 5815 RVA: 0x0006B054 File Offset: 0x00069254 private static Class_499.ConvertResult TryConvertInternal(object initialValue, CultureInfo culture, Type targetType, out object value) { if (initialValue == null) { throw new ArgumentNullException("initialValue"); } if (Class_514.IsNullableType(targetType)) { targetType = Nullable.GetUnderlyingType(targetType); } Type type = initialValue.GetType(); if (targetType == type) { value = initialValue; return(Class_499.ConvertResult.Success); } if (Class_499.IsConvertible(initialValue.GetType()) && Class_499.IsConvertible(targetType)) { if (targetType.IsEnum) { if (initialValue is string) { value = Enum.Parse(targetType, initialValue.ToString(), true); return(Class_499.ConvertResult.Success); } if (Class_499.IsInteger(initialValue)) { value = Enum.ToObject(targetType, initialValue); return(Class_499.ConvertResult.Success); } } value = System.Convert.ChangeType(initialValue, targetType, culture); return(Class_499.ConvertResult.Success); } if (initialValue is DateTime && targetType == typeof(DateTimeOffset)) { value = new DateTimeOffset((DateTime)initialValue); return(Class_499.ConvertResult.Success); } if (initialValue is byte[] && targetType == typeof(Guid)) { value = new Guid((byte[])initialValue); return(Class_499.ConvertResult.Success); } if (initialValue is Guid && targetType == typeof(byte[])) { value = ((Guid)initialValue).ToByteArray(); return(Class_499.ConvertResult.Success); } if (initialValue is string) { if (targetType == typeof(Guid)) { value = new Guid((string)initialValue); return(Class_499.ConvertResult.Success); } if (targetType == typeof(Uri)) { value = new Uri((string)initialValue, UriKind.RelativeOrAbsolute); return(Class_499.ConvertResult.Success); } if (targetType == typeof(TimeSpan)) { value = Class_499.ParseTimeSpan((string)initialValue); return(Class_499.ConvertResult.Success); } if (targetType == typeof(byte[])) { value = System.Convert.FromBase64String((string)initialValue); return(Class_499.ConvertResult.Success); } if (typeof(Type).IsAssignableFrom(targetType)) { value = Type.GetType((string)initialValue, true); return(Class_499.ConvertResult.Success); } } if (targetType == typeof(BigInteger)) { value = Class_499.ToBigInteger(initialValue); return(Class_499.ConvertResult.Success); } if (initialValue is BigInteger) { value = Class_499.FromBigInteger((BigInteger)initialValue, targetType); return(Class_499.ConvertResult.Success); } TypeConverter typeConverter = Class_470.GetTypeConverter(type); if (typeConverter != null && typeConverter.CanConvertTo(targetType)) { value = typeConverter.ConvertTo(null, culture, initialValue, targetType); return(Class_499.ConvertResult.Success); } TypeConverter typeConverter2 = Class_470.GetTypeConverter(targetType); if (typeConverter2 != null && typeConverter2.CanConvertFrom(type)) { value = typeConverter2.ConvertFrom(null, culture, initialValue); return(Class_499.ConvertResult.Success); } if (initialValue == DBNull.Value) { if (Class_514.IsNullable(targetType)) { value = Class_499.EnsureTypeAssignable(null, type, targetType); return(Class_499.ConvertResult.Success); } value = null; return(Class_499.ConvertResult.CannotConvertNull); } else { if (initialValue is INullable) { value = Class_499.EnsureTypeAssignable(Class_499.ToValue((INullable)initialValue), type, targetType); return(Class_499.ConvertResult.Success); } if (targetType.IsInterface || targetType.IsGenericTypeDefinition || targetType.IsAbstract) { value = null; return(Class_499.ConvertResult.NotInstantiableType); } value = null; return(Class_499.ConvertResult.NoValidConversion); } }