public static TEnum?ObjToNullableEnum <TEnum>(object obj) where TEnum : struct, Enum { var enumStr = StringConv.ObjectSafeToString(obj); if (StringEnumDeterminer <TEnum> .Is(enumStr)) { return(StringEnumDeterminer <TEnum> .To(enumStr)); } return(null); }
public static object ObjToNullableEnum(object obj, Type enumType) { var enumStr = StringConv.ObjectSafeToString(obj); if (StringEnumDeterminer.Is(enumStr, enumType)) { return(StringEnumDeterminer.To(enumStr, enumType)); } return(null); }
private static bool FromNumericTypeToString <N>(N numericVal, CastingContext context, string defaultStr, out object result) { var valueUpdated = true; result = defaultStr; switch (numericVal) { case short _1: result = StringConv.Int16ToString(_1, defaultStr); break; case ushort _2: result = StringConv.UInt16ToString(_2, defaultStr); break; case int _3: result = StringConv.Int32ToString(_3, defaultStr); break; case uint _4: result = StringConv.UInt32ToString(_4, defaultStr); break; case long _5: result = StringConv.Int64ToString(_5, defaultStr); break; case ulong _6: result = StringConv.UInt64ToString(_6, defaultStr); break; case float _7: result = StringConv.FloatToString(_7, context.Digits); break; case double _8: result = StringConv.DoubleToString(_8, context.Digits); break; case decimal _9: result = StringConv.DecimalToString(_9, context.Digits, defaultStr); break; default: valueUpdated = false; break; } return(valueUpdated); }
private static bool FromNullableNumericTypeToString(object numericVal, Type oType, CastingContext context, out object result) { var valueUpdated = true; result = null; if (oType == TypeClass.Int16NullableClass) { result = StringConv.Int16ToString(NumericConv.ObjectToNullableInt16(numericVal), string.Empty); } else if (oType == TypeClass.UInt16NullableClass) { result = StringConv.UInt16ToString(NumericConv.ObjectToNullableUInt16(numericVal), string.Empty); } else if (oType == TypeClass.Int32NullableClass) { result = StringConv.Int32ToString(NumericConv.ObjectToNullableInt32(numericVal), string.Empty); } else if (oType == TypeClass.UInt32NullableClass) { result = StringConv.UInt32ToString(NumericConv.ObjectToNullableUInt32(numericVal), string.Empty); } else if (oType == TypeClass.Int64NullableClass) { result = StringConv.Int64ToString(NumericConv.ObjectToNullableInt64(numericVal), string.Empty); } else if (oType == TypeClass.UInt64NullableClass) { result = StringConv.UInt64ToString(NumericConv.ObjectToNullableUInt64(numericVal), string.Empty); } else if (oType == TypeClass.FloatNullableClass) { result = StringConv.FloatToString(NumericConv.ObjectToNullableFloat(numericVal), context.Digits); } else if (oType == TypeClass.DoubleNullableClass) { result = StringConv.DoubleToString(NumericConv.ObjectToNullableDouble(numericVal), context.Digits); } else if (oType == TypeClass.DecimalNullableClass) { result = StringConv.DecimalToString(NumericConv.ObjectToNullableDecimal(numericVal), context.Digits, string.Empty); } else { valueUpdated = false; } return(valueUpdated); }
private static bool FromObjToNullableTypeProxy(object fromObj, Type xType, out object result) { var innerType = Nullable.GetUnderlyingType(xType); if (innerType == TypeClass.StringClass) { result = StringConv.ObjectSafeToString(fromObj); return(true); } if (TypeHelper.IsNumericType(xType)) { return(FromObjToNullableNumericType(fromObj, xType, out result)); } if (TypeHelper.IsEnumType(innerType)) { result = EnumConv.ObjToNullableEnum(fromObj, innerType); } if (TypeHelper.IsDateTimeTypes(innerType)) { return(FromObjToNullableDateTimeType(fromObj, innerType, out result)); } if (TypeHelper.IsGuidType(innerType)) { result = GuidConv.ObjToNullableGuid(fromObj); return(true); } if (innerType == TypeClass.ObjectClass) { result = fromObj; return(true); } result = null; return(false); }
private static bool FromObjToTypeProxy <X>(object fromObj, X defaultVal, Type xType, out object result) { if (defaultVal is string defaultStr) { result = StringConv.ObjectSafeToString(fromObj, defaultStr); return(true); } if (TypeHelper.IsNumericType(xType)) { return(FromObjToNumericType(fromObj, defaultVal, xType, out result)); } if (TypeHelper.IsEnumType(xType)) { result = EnumConv.ObjToEnum(fromObj, xType, defaultVal); return(true); } if (TypeHelper.IsDateTimeTypes(xType)) { return(FromObjToDateTime(fromObj, defaultVal, xType, out result)); } if (defaultVal is Guid defaultGuid) { result = GuidConv.ObjToGuid(fromObj, defaultGuid); return(true); } if (xType == TypeClass.ObjectClass) { result = defaultVal; return(true); } result = defaultVal; return(false); }
public static TEnum ObjToEnum <TEnum>(object obj, TEnum defaultVal = default) where TEnum : struct, Enum { return(StringEnumDeterminer <TEnum> .To(StringConv.ObjectSafeToString(obj), defaultVal : defaultVal)); }
public static object ObjToEnum(object obj, Type enumType, object defaultVal = default) { return(StringEnumDeterminer.To(StringConv.ObjectSafeToString(obj), enumType, defaultVal: defaultVal)); }
private static bool FromGuidToString(Guid guid, CastingContext context, string defaultStr, out object result) { result = StringConv.GuidToString(guid, context, defaultStr); return(true); }
private static bool FromNullableGuidToString(Guid?guid, CastingContext context, out object result) { result = StringConv.GuidToString(guid, context); return(true); }