Ejemplo n.º 1
0
        public void IsNullableType()
        {
            Assert.True(TypeJudgment.IsNullableType(typeof(int?)));
            Assert.True(TypeJudgment.IsNullableType(typeof(Nullable <System.Int32>)));

            Assert.True(TypeJudgment.IsNullableType(typeof(int?).GetTypeInfo()));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Invoke
        /// </summary>
        /// <param name="context"></param>
        /// <param name="next"></param>
        /// <returns></returns>
        /// <exception cref="ArgumentInvalidException"></exception>
        public override Task Invoke(ParameterAspectContext context, ParameterAspectDelegate next)
        {
            var condition = MayBeNullable
                ? TypeJudgment.IsNumericType(context.Parameter.Type)
                : TypeJudgment.IsNumericType(context.Parameter.Type) && !TypeJudgment.IsNullableType(context.Parameter.Type);

            AssertionJudgment.Require2Validation <ArgumentInvalidException>(condition,
                                                                            Message, context.Parameter.Name);
            return(next(context));
        }
Ejemplo n.º 3
0
        public override Task Invoke(ParameterAspectContext context, ParameterAspectDelegate next)
        {
            var condition = MayBeNullable
                ? TypeJudgment.IsNumericType(context.Parameter.Type)
                : TypeJudgment.IsNumericType(context.Parameter.Type) && !TypeJudgment.IsNullableType(context.Parameter.Type);

            if (condition)
            {
                throw new ArgumentException(Message, context.Parameter.Name);
            }
            return(next(context));
        }
Ejemplo n.º 4
0
        public void DatAndReadcgiTest()
        {
            BBSType bt;

            for (int i = 0; i < dats.Length; i++)
            {
                bt = TypeJudgment.BBSTypeJudg(dats[i]);
                Assert.AreEqual <BBSType>(bt, TypeJudgment.BBSTypeJudg(reads[i]));
                string read = URLParse.DatToReadcgi(dats[i], bt);
                string dat  = URLParse.ReadcgiToDat(reads[i], bt);
                Assert.AreEqual <string>(read, reads[i]);
                Assert.AreEqual <string>(dat, dats[i]);
            }
        }
Ejemplo n.º 5
0
        public void IsNumericTest()
        {
            Assert.True(TypeJudgment.IsNumericType(1.GetType()));
            Assert.True(TypeJudgment.IsNumericType(1D.GetType()));
            Assert.True(TypeJudgment.IsNumericType(1F.GetType()));
            Assert.True(TypeJudgment.IsNumericType(1M.GetType()));
            Assert.True(TypeJudgment.IsNumericType(1.001M.GetType()));
            Assert.True(TypeJudgment.IsNumericType(1L.GetType()));

            Assert.True(TypeJudgment.IsNumericType(1.GetType().GetTypeInfo()));
            Assert.True(TypeJudgment.IsNumericType(1D.GetType().GetTypeInfo()));
            Assert.True(TypeJudgment.IsNumericType(1F.GetType().GetTypeInfo()));
            Assert.True(TypeJudgment.IsNumericType(1M.GetType().GetTypeInfo()));
            Assert.True(TypeJudgment.IsNumericType(1.001M.GetType().GetTypeInfo()));
            Assert.True(TypeJudgment.IsNumericType(1L.GetType().GetTypeInfo()));
        }
Ejemplo n.º 6
0
        public void TypeJudgementTest()
        {
            Type    t;
            BBSType bt;
            int     i = 0;

            foreach (string item in datas)
            {
                TypeJudgment.AllJudg(item, out bt, out t);
                Assert.AreEqual <Type>(t, TypeJudgment.TypeJudg(item));
                Assert.AreEqual <BBSType>(bt, TypeJudgment.BBSTypeJudg(item));
                // throgh debug and this method hasn't bugs.
                //Assert.AreEqual<Type>(t,types[i]);
                //Assert.AreEqual(bt, bbsTypes[i]);
                i++;
            }
        }
Ejemplo n.º 7
0
 /// <summary>
 /// 将 可空类型 安全转换为 非可空基础类型
 /// </summary>
 /// <param name="type">类型</param>
 public static Type ToSafeNonNullableType(Type type) =>
 TypeJudgment.IsNullableType(type) ? ToNonNullableType(type) : type;
Ejemplo n.º 8
0
 /// <summary>
 /// Is numeric type
 /// </summary>
 /// <param name="type"></param>
 /// <returns></returns>
 public static bool IsNumericType(Type type) => TypeJudgment.IsNumericType(type);
Ejemplo n.º 9
0
 /// <summary>
 /// To judge the given type is assignable to the generic type or not.
 /// </summary>
 /// <param name="type">The given type</param>
 /// <param name="genericType">The generic type</param>
 /// <returns></returns>
 public static bool IsGenericImplementation(Type type, Type genericType) => TypeJudgment.IsGenericImplementation(type, genericType);
Ejemplo n.º 10
0
 /// <summary>
 /// To judge the given type is assignable to the generic type or not.
 /// </summary>
 /// <typeparam name="TGot">The given type TGot</typeparam>
 /// <typeparam name="TGeneric">The generic type TGeneric</typeparam>
 /// <returns></returns>
 public static bool IsGenericImplementation <TGot, TGeneric>() => TypeJudgment.IsGenericImplementation <TGot, TGeneric>();
Ejemplo n.º 11
0
 /// <summary>
 /// Is nullable type
 /// </summary>
 /// <param name="type"></param>
 /// <returns></returns>
 public static bool IsNullableType(Type type) => TypeJudgment.IsNullableType(type);
Ejemplo n.º 12
0
 /// <summary>
 /// Is nullable type
 /// </summary>
 /// <param name="typeInfo"></param>
 /// <returns></returns>
 public static bool IsNullableType(TypeInfo typeInfo) => TypeJudgment.IsNullableType(typeInfo);
Ejemplo n.º 13
0
 public static bool IsNullableGuidType(Type type)
 {
     return(TypeJudgment.IsNullableType(type) && IsGuidType(Nullable.GetUnderlyingType(type)));
 }
Ejemplo n.º 14
0
 public static TypeInfo ToSafeNonNullableTypeInfo(TypeInfo typeInfo)
 {
     return(TypeJudgment.IsNullableType(typeInfo) ? ToNonNullableTypeInfo(typeInfo) : typeInfo);
 }
Ejemplo n.º 15
0
 /// <summary>
 /// Is enum type
 /// </summary>
 /// <param name="mayNullable"></param>
 /// <typeparam name="T"></typeparam>
 /// <returns></returns>
 public static bool IsEnumType <T>(bool mayNullable = false) => TypeJudgment.IsEnumType <T>(mayNullable);
Ejemplo n.º 16
0
 /// <summary>
 /// Is numeric type
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <returns></returns>
 public static bool IsNumericType <T>() => TypeJudgment.IsNumericType <T>();
Ejemplo n.º 17
0
 /// <summary>
 /// Is enum type
 /// </summary>
 /// <param name="typeInfo"></param>
 /// <param name="mayNullable"></param>
 /// <returns></returns>
 public static bool IsEnumType(TypeInfo typeInfo, bool mayNullable = false) => TypeJudgment.IsEnumType(typeInfo, mayNullable);
Ejemplo n.º 18
0
 public static Type ToSafeNonNullableType(Type type)
 {
     return(TypeJudgment.IsNullableType(type) ? ToNonNullableType(type) : type);
 }
Ejemplo n.º 19
0
 /// <summary>
 /// Is numeric type
 /// </summary>
 /// <param name="typeInfo"></param>
 /// <returns></returns>
 public static bool IsNumericType(TypeInfo typeInfo) => TypeJudgment.IsNumericType(typeInfo);
Ejemplo n.º 20
0
        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 = TypeJudgment.IsNullableType(oType);
            var xTypeNullableFlag = TypeJudgment.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 (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 (TypeHelper.IsEnumType(oType))
            {
                return(FromEnumTo(oType, from, context, xType, xTypeNullableFlag, defaultVal));
            }

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

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

            if (TypeHelper.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.ObjectClass)
            {
                return(FromObjTo(from, context, xType, xTypeNullableFlag, defaultVal));
            }

            if (xType == TypeClass.ObjectClass)
            {
                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(Mapper.DefaultMapper.Instance.MapTo <O, X>(from));
                }
                catch
                {
                    return(xTypeNullableFlag ? default : defaultVal);
                }
            }
        }
Ejemplo n.º 21
0
 /// <summary>
 /// Is nullable type
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <returns></returns>
 public static bool IsNullableType <T>() => TypeJudgment.IsNullableType <T>();