private static bool FromStringToNullableTypeProxy(string from, Type xType, out object result) { var innerType = Nullable.GetUnderlyingType(xType); if (TypeHelper.IsNumericType(innerType)) { return(FromStringToNullableNumericType(from, innerType, out result)); } if (innerType == TypeClass.GuidClass) { return(FromStringToNullableGuid(from, out result)); } if (TypeHelper.IsDateTimeTypes(innerType)) { return(FromStringToNullableDateTimeType(from, innerType, out result)); } if (TypeHelper.IsEnumType(xType)) { return(FromStringToNullableEnum(from, innerType, out result)); } if (innerType == TypeClass.ObjectClass) { result = from; return(true); } var _ = TypeDefault.Of(xType); result = from.Is(xType, IgnoreCase.FALSE, t => _ = t) ? _ : null; return(false); }
private static bool FromStringToTypeProxy(string from, object defaultVal, Type xType, out object result) { if (TypeHelper.IsNumericType(xType)) { return(FromStringToNumericType(from, defaultVal, xType, out result)); } if (xType == TypeClass.GuidClass) { return(FromStringToGuid(from, defaultVal, out result)); } if (TypeHelper.IsDateTimeTypes(xType)) { return(FromStringToDateTimeType(from, defaultVal, xType, out result)); } if (TypeHelper.IsEnumType(xType)) { return(FromStringToEnum(from, xType, defaultVal, out result)); } if (xType == TypeClass.ObjectClass) { result = from; return(true); } var _ = TypeDefault.Of(xType); result = from.Is(xType, IgnoreCase.FALSE, t => _ = t) ? _ : defaultVal; return(false); }
/// <summary> /// Get default value of special type T. /// </summary> /// <typeparam name="T"></typeparam> /// <returns></returns> public static T DefaultValue <T>() => TypeDefault.Of <T>();
/// <summary> /// Create instance /// </summary> /// <typeparam name="TInstance">Special type you need to return.</typeparam> /// <param name="type">Special type</param> /// <param name="args">Arguments for such type's constructor</param> /// <returns>Instance of special type</returns> public static TInstance CreateInstance <TInstance>(Type type, params object[] args) { return(CreateInstance(type, args) is TInstance ret ? ret : TypeDefault.Of <TInstance>()); }
public static TValue TryTo <TValue>(this object obj) { return(obj.TryTo(TypeDefault.Of <TValue>())); }