コード例 #1
0
        private static bool FromStringToTypeProxy <X>(string from, X defaultVal, Type xType, out object result)
        {
            if (TypeDeterminer.IsNumericType(xType))
            {
                return(FromStringToNumericType(from, defaultVal, xType, out result));
            }
            if (TypeDeterminer.IsGuidType(xType))
            {
                return(FromStringToGuid(from, defaultVal, out result));
            }
            if (TypeDeterminer.IsDateTimeTypes(xType))
            {
                return(FromStringToDateTimeType(from, defaultVal, xType, out result));
            }
            if (TypeDeterminer.IsEnumType(xType))
            {
                return(FromStringToEnum(from, defaultVal, out result));
            }
            if (TypeDeterminer.IsOriginObject(xType))
            {
                result = defaultVal;
                return(true);
            }

            object _ = default(X);

            result = from.Is(typeof(X), t => _ = t) ? _ : defaultVal;
            return(false);
        }
コード例 #2
0
 /// <summary>
 /// Cast <see cref="object"/> to given type.
 /// </summary>
 /// <param name="obj"></param>
 /// <param name="targetType"></param>
 /// <returns></returns>
 public static object CastTo(this object obj, Type targetType)
 {
     CastTypeHelper.Guard(targetType, nameof(targetType));
     return(obj is null
         ? default
         : XConv.To(obj, obj.GetType(), targetType, TypeDeterminer.GetDefaultValue(targetType)));
 }
コード例 #3
0
        private static bool FromStringToTypeProxy(string from, object defaultVal, Type xType, out object result)
        {
            if (TypeDeterminer.IsNumericType(xType))
            {
                return(FromStringToNumericType(from, defaultVal, xType, out result));
            }
            if (TypeDeterminer.IsGuidType(xType))
            {
                return(FromStringToGuid(from, defaultVal, out result));
            }
            if (TypeDeterminer.IsDateTimeTypes(xType))
            {
                return(FromStringToDateTimeType(from, defaultVal, xType, out result));
            }
            if (TypeDeterminer.IsEnumType(xType))
            {
                return(FromStringToEnum(from, xType, defaultVal, out result));
            }
            if (TypeDeterminer.IsOriginObject(xType))
            {
                result = from;
                return(true);
            }

            var _ = TypeDeterminer.GetDefaultValue(xType);

            result = from.Is(xType, t => _ = t) ? _ : defaultVal;
            return(false);
        }
コード例 #4
0
        private static bool FromStringToNullableTypeProxy(string from, Type xType, out object result)
        {
            var innerType = TypeConv.GetNonNullableType(xType);

            if (TypeDeterminer.IsNumericType(innerType))
            {
                return(FromStringToNullableNumericType(from, innerType, out result));
            }
            if (TypeDeterminer.IsGuidType(innerType))
            {
                return(FromStringToNullableGuid(from, out result));
            }
            if (TypeDeterminer.IsDateTimeTypes(innerType))
            {
                return(FromStringToNullableDateTimeType(from, innerType, out result));
            }
            if (TypeDeterminer.IsEnumType(xType))
            {
                return(FromStringToNullableEnum(from, innerType, out result));
            }
            if (TypeDeterminer.IsOriginObject(innerType))
            {
                result = from;
                return(true);
            }

            var _ = TypeDeterminer.GetDefaultValue(xType);

            result = from.Is(xType, t => _ = t) ? _ : null;
            return(false);
        }
コード例 #5
0
        public void JTokensAreConvertedToAny(Type input)
        {
            var sut = new TypeDeterminer(CreatePropertySettings(), enumSettings, namespaceSettings);

            var actual = sut.Determine(input);

            Assert.That(actual.FormattedType, Is.EqualTo("any"));
            Assert.That(actual.Dependencies, Is.Empty);
        }
コード例 #6
0
        public void PrimitiveTypesAsExpected(Type input, string expected)
        {
            var sut = new TypeDeterminer(CreatePropertySettings(), enumSettings, namespaceSettings);

            var actual = sut.Determine(input);

            Assert.That(actual.FormattedType, Is.EqualTo(expected));
            Assert.That(actual.Dependencies, Is.Empty);
        }
コード例 #7
0
        public void DictionaryAsExpected()
        {
            var input = typeof(Dictionary <int, string>);
            var sut   = new TypeDeterminer(CreatePropertySettings(), enumSettings, namespaceSettings);

            var actual = sut.Determine(input);

            Assert.That(actual.FormattedType, Is.EqualTo("{ [key: number]: string }"));
            Assert.That(actual.Dependencies, Is.Empty);
        }
コード例 #8
0
        public void GenericTypeIsTranslatedToGeneric()
        {
            var input = typeof(GenericClass <string, Product>);
            var sut   = new TypeDeterminer(CreatePropertySettings(), enumSettings, namespaceSettings);

            var actual = sut.Determine(input);

            Assert.That(actual.FormattedType, Is.EqualTo("TypescriptGenerator.Test.GenericClass<string,TestObjects.Product>"));
            Assert.That(actual.Dependencies, Is.EquivalentTo(new [] { typeof(GenericClass <,>), typeof(Product) }));
        }
コード例 #9
0
 private static bool FromBooleanToNumericType <N>(bool oVal, CastingContext context, Type xType, out object result)
 {
     if (TypeDeterminer.IsByteClass(xType))
     {
         result = oVal.ToBinary();
     }
     else
     {
         result = oVal ? context.NumericTrue.As <N>() : context.NumericFalse.As <N>();
     }
     return(true);
 }
コード例 #10
0
        public void CollectionAsExpected()
        {
            var input = typeof(List <string>);
            var nullableStringConverter = new GenericTypeConverter(x => x == typeof(string), x => "string | null");
            var propertySettings        = CreatePropertySettings();

            propertySettings.TypeConverters.Add(nullableStringConverter);
            var sut = new TypeDeterminer(propertySettings, enumSettings, namespaceSettings);

            var actual = sut.Determine(input);

            Assert.That(actual.FormattedType, Is.EqualTo("(string | null)[]"));
            Assert.That(actual.Dependencies, Is.Empty);
        }
コード例 #11
0
        private static bool FromBooleanToNullableNumericType(bool oVal, CastingContext context, Type innerType, out object result)
        {
            if (TypeDeterminer.IsByteClass(innerType))
            {
                result = oVal.ToBinary();
            }
            else
            {
                var midNumeric = oVal ? context.NumericTrue : context.NumericFalse;
                result = Convert.ChangeType(midNumeric, innerType);
            }

            return(true);
        }
コード例 #12
0
        private static bool FromGuidToTypeProxy(Guid from, object defaultVal, CastingContext context, Type xType, out object result)
        {
            if (defaultVal is string defaultStr)
            {
                return(FromGuidToString(from, context, defaultStr, out result));
            }
            if (TypeDeterminer.IsOriginObject(xType))
            {
                result = from;
                return(true);
            }

            result = defaultVal;
            return(false);
        }
コード例 #13
0
        private static bool FromGuidToNullableTypeProxy(Guid from, CastingContext context, Type xType, out object result)
        {
            var innerType = TypeConv.GetNonNullableType(xType);

            if (innerType == TypeClass.StringClazz)
            {
                return(FromGuidToString(from, context, string.Empty, out result));
            }
            if (TypeDeterminer.IsOriginObject(innerType))
            {
                result = from;
                return(true);
            }

            result = null;
            return(false);
        }
コード例 #14
0
        private static bool FromDateTimeToTypeProxy(DateTime dateTime, object defaultVal, CastingContext context, Type xType, out object result)
        {
            if (TypeDeterminer.IsStringType(defaultVal, out var givenFormat))
            {
                return(FromDateTimeToString(dateTime, givenFormat, context, out result));
            }
            if (TypeDeterminer.IsNumericType(xType))
            {
                return(FromDateTimeToNumericType(dateTime, defaultVal, xType, out result));
            }
            if (TypeDeterminer.IsOriginObject(xType))
            {
                result = dateTime;
                return(true);
            }

            result = defaultVal;
            return(false);
        }
コード例 #15
0
        private static bool FromEnumToTypeProxy(Type enumType, object enumVal, object defaultVal, CastingContext context, Type xType, out object result)
        {
            if (TypeDeterminer.IsStringType(defaultVal, out var defaultStr))
            {
                return(FromEnumToString(enumType, enumVal, defaultStr, out result));
            }
            if (TypeDeterminer.IsNumericType(xType))
            {
                return(FromEnumToNumericType(enumType, enumVal, defaultVal, xType, out result));
            }
            if (TypeDeterminer.IsOriginObject(xType))
            {
                result = enumVal;
                return(true);
            }

            result = defaultVal;
            return(false);
        }
コード例 #16
0
        private static bool FromNumericTypeToTypeProxy <X>(object oVal, X defaultVal, CastingContext context, Type xType, out object result)
        {
            if (TypeDeterminer.IsStringType(defaultVal, out var defaultStr))
            {
                return(FromNumericTypeToString(oVal, context, defaultStr, out result));
            }
            if (TypeDeterminer.IsNumericType(xType))
            {
                return(FromNumericTypeToNumericType(oVal, defaultVal, xType, out result));
            }
            if (TypeDeterminer.IsOriginObject(xType))
            {
                result = oVal;
                return(true);
            }

            result = defaultVal;
            return(false);
        }
コード例 #17
0
        private static bool FromBooleanToTypeProxy(bool oVal, object defaultVal, CastingContext context, Type xType, out object result)
        {
            if (TypeDeterminer.IsStringType(xType))
            {
                return(FromBooleanToString(oVal, context, out result));
            }
            if (TypeDeterminer.IsNumericType(xType))
            {
                return(FromBooleanToNumericType(oVal, context, xType, out result));
            }
            if (TypeDeterminer.IsOriginObject(xType))
            {
                result = oVal;
                return(true);
            }

            result = defaultVal;
            return(false);
        }
コード例 #18
0
        private static bool FromDateTimeToNullableTypeProxy(DateTime dateTime, CastingContext context, Type xType, out object result)
        {
            var innerType = TypeConv.GetNonNullableType(xType);

            if (TypeDeterminer.IsStringType(innerType))
            {
                return(FromDateTimeToNullableString(dateTime, context, out result));
            }
            if (TypeDeterminer.IsNumericType(innerType))
            {
                return(FromDateTimeToNullableNumericType(dateTime, innerType, out result));
            }
            if (TypeDeterminer.IsOriginObject(innerType))
            {
                result = dateTime;
                return(true);
            }

            result = null;
            return(false);
        }
コード例 #19
0
        private static bool FromNullableNumericTypeToTypeProxy(Type oType, object oVal, CastingContext context, Type xType, out object result)
        {
            var innerType = TypeConv.GetNonNullableType(xType);

            if (TypeDeterminer.IsStringType(innerType))
            {
                return(FromNullableNumericTypeToString(oVal, oType, context, out result));
            }
            if (TypeDeterminer.IsNumericType(innerType))
            {
                return(FromNumericTypeToNullableNumericType(oVal, innerType, out result));
            }
            if (TypeDeterminer.IsOriginObject(innerType))
            {
                result = oVal;
                return(true);
            }

            result = null;
            return(false);
        }
コード例 #20
0
        private static bool FromObjToNullableTypeProxy(object fromObj, Type xType, out object result)
        {
            var innerType = TypeConv.GetNonNullableType(xType);

            if (TypeDeterminer.IsStringType(innerType))
            {
                result = StrConvX.ObjectSafeToString(fromObj);
                return(true);
            }

            if (TypeDeterminer.IsNumericType(innerType))
            {
                return(FromObjToNullableNumericType(fromObj, innerType, out result));
            }

            if (TypeDeterminer.IsEnumType(innerType))
            {
                result = EnumConvX.ObjToNullableEnum(fromObj, innerType);
            }

            if (TypeDeterminer.IsDateTimeTypes(innerType))
            {
                return(FromObjToNullableDateTimeType(fromObj, innerType, out result));
            }

            if (TypeDeterminer.IsGuidType(innerType))
            {
                result = GuidConvX.ObjToNullableGuid(fromObj);
                return(true);
            }

            if (TypeDeterminer.IsOriginObject(innerType))
            {
                result = fromObj;
                return(true);
            }

            result = null;
            return(false);
        }
コード例 #21
0
        private static bool FromObjToTypeProxy <X>(object fromObj, X defaultVal, Type xType, out object result)
        {
            if (TypeDeterminer.IsStringType(defaultVal, out var defaultStr))
            {
                result = StrConvX.ObjectSafeToString(fromObj, defaultStr);
                return(true);
            }

            if (TypeDeterminer.IsNumericType(xType))
            {
                return(FromObjToNumericType(fromObj, defaultVal, xType, out result));
            }

            if (TypeDeterminer.IsEnumType(xType))
            {
                result = EnumConvX.ObjToEnum(fromObj, xType, defaultVal);
                return(true);
            }

            if (TypeDeterminer.IsDateTimeTypes(xType))
            {
                return(FromObjToDateTime(fromObj, defaultVal, xType, out result));
            }

            if (defaultVal is Guid defaultGuid)
            {
                result = GuidConvX.ObjToGuid(fromObj, defaultGuid);
                return(true);
            }

            if (TypeDeterminer.IsOriginObject(xType))
            {
                result = defaultVal;
                return(true);
            }

            result = defaultVal;
            return(false);
        }
コード例 #22
0
ファイル: XConv.cs プロジェクト: cosmos-loops/cosmos-standard
        public static X To <O, X>(O from, X defaultVal = default, CastingContext context = null, IObjectMapper mapper = null)
        {
            var oType             = typeof(O);
            var xType             = typeof(X);
            var oTypeNullableFlag = Types.IsNullableType(oType);
            var xTypeNullableFlag = Types.IsNullableType(xType);

            context ??= CastingContext.DefaultContext;

            if (xType.IsValueType && defaultVal is null)
            {
                defaultVal = Activator.CreateInstance <X>();
            }

            if (from is null)
            {
                return(defaultVal);
            }

            if (oType == xType)
            {
                return(from.AsOrDefault(defaultVal));
            }

            if (CustomConvertManager.TryGetConvertHandler(oType, xType, out var handler))
            {
                return((handler?.Invoke(from)).As <X>() ?? defaultVal);
            }

            if (from is string strFrom)
            {
                return(FromStringTo(strFrom, xType, xTypeNullableFlag, defaultVal));
            }

            if (from is DateTime dtFrom)
            {
                return(FromDateTimeTo(dtFrom, context, xType, xTypeNullableFlag, defaultVal));
            }

            if (from is bool boolFrom)
            {
                return(FromBooleanTo(boolFrom, context, xType, xTypeNullableFlag, defaultVal));
            }

            if (TypeDeterminer.IsEnumType(oType))
            {
                return(FromEnumTo(oType, from, context, xType, xTypeNullableFlag, defaultVal));
            }

            if (TypeDeterminer.IsNullableNumericType(oType) || Types.IsNumericType(oType))
            {
                return(FromNumericTo(oType, oTypeNullableFlag, from, context, xType, xTypeNullableFlag, defaultVal));
            }

            if (from is Guid guid)
            {
                return(FromGuidTo(guid, context, xType, xTypeNullableFlag, defaultVal));
            }

            if (TypeDeterminer.IsNullableGuidType(oType))
            {
                return(FromNullableGuidTo(from.As <Guid?>(), context, xType, xTypeNullableFlag, defaultVal));
            }

            if (from is IConvertible)
            {
                return(Convert.ChangeType(from, xType).As <X>());
            }

            if (oType == TypeClass.ObjectClazz)
            {
                return(FromObjTo(from, context, xType, xTypeNullableFlag, defaultVal));
            }

            if (xType == TypeClass.ObjectClazz)
            {
                return(from.As <X>());
            }

            if (xType.IsAssignableFrom(oType))
            {
                return(from.As <X>());
            }

            if (mapper != null)
            {
                return(mapper.MapTo <O, X>(from));
            }

            try
            {
                return(from.As <X>());
            }
            catch
            {
                try
                {
                    return(DefaultMapper.Instance.MapTo <O, X>(from));
                }
                catch
                {
                    return(xTypeNullableFlag ? default : defaultVal);
                }
            }
        }
コード例 #23
0
ファイル: XConv.cs プロジェクト: cosmos-loops/cosmos-standard
        public static object To(object from, Type sourceType, Type targetType,
                                object defaultVal = default, CastingContext context = null, IObjectMapper mapper = null)
        {
            var oType             = sourceType;
            var xType             = targetType;
            var oTypeNullableFlag = Types.IsNullableType(oType);
            var xTypeNullableFlag = Types.IsNullableType(xType);

            context ??= CastingContext.DefaultContext;

            if (xType.IsValueType && defaultVal is null)
            {
                defaultVal = Activator.CreateInstance(xType);
            }

            if (from is null)
            {
                return(defaultVal);
            }

            if (oType == xType)
            {
                return(from.AsOrDefault(defaultVal));
            }

            if (from is string strFrom)
            {
                return(FromStringTo(strFrom, xType, xTypeNullableFlag, defaultVal));
            }

            if (from is DateTime dtFrom)
            {
                return(FromDateTimeTo(dtFrom, context, xType, xTypeNullableFlag, defaultVal));
            }

            if (from is bool boolFrom)
            {
                return(FromBooleanTo(boolFrom, context, xType, xTypeNullableFlag, defaultVal));
            }

            if (TypeDeterminer.IsEnumType(oType))
            {
                return(FromEnumTo(oType, from, context, xType, xTypeNullableFlag, defaultVal));
            }

            if (TypeDeterminer.IsNullableNumericType(oType) || Types.IsNumericType(oType))
            {
                return(FromNumericTo(oType, oTypeNullableFlag, from, context, xType, xTypeNullableFlag, defaultVal));
            }

            if (from is Guid guid)
            {
                return(FromGuidTo(guid, context, xType, xTypeNullableFlag, defaultVal));
            }

            if (TypeDeterminer.IsNullableGuidType(oType))
            {
                return(FromNullableGuidTo(from.As <Guid?>(), context, xType, xTypeNullableFlag, defaultVal));
            }

            if (from is IConvertible)
            {
                return(Convert.ChangeType(from, xType));
            }

            if (oType == TypeClass.ObjectClazz)
            {
                return(FromObjTo(from, oType, context, xType, xTypeNullableFlag, defaultVal));
            }

            if (xType == TypeClass.ObjectClazz)
            {
                return(Convert.ChangeType(from, xType));
            }

            if (xType.IsAssignableFrom(oType))
            {
                return(from);
            }

            if (mapper != null)
            {
                return(mapper.MapTo(oType, xType, from));
            }

            try
            {
                return(Convert.ChangeType(from, xType, context.FormatProvider).AsOrDefault(defaultVal));
            }
            catch
            {
                try
                {
                    return(DefaultMapper.Instance.MapTo(oType, xType, from));
                }
                catch
                {
                    return(xTypeNullableFlag ? default : defaultVal);
                }
            }
        }