示例#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()));
        }
示例#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));
        }
        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));
        }
示例#4
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);
                }
            }
        }
示例#5
0
 public static TypeInfo ToSafeNonNullableTypeInfo(TypeInfo typeInfo)
 {
     return(TypeJudgment.IsNullableType(typeInfo) ? ToNonNullableTypeInfo(typeInfo) : typeInfo);
 }
示例#6
0
 public static Type ToSafeNonNullableType(Type type)
 {
     return(TypeJudgment.IsNullableType(type) ? ToNonNullableType(type) : type);
 }
示例#7
0
 /// <summary>
 /// 将 可空类型 安全转换为 非可空基础类型
 /// </summary>
 /// <param name="type">类型</param>
 public static Type ToSafeNonNullableType(Type type) =>
 TypeJudgment.IsNullableType(type) ? ToNonNullableType(type) : type;
示例#8
0
 /// <summary>
 /// Is nullable type
 /// </summary>
 /// <param name="typeInfo"></param>
 /// <returns></returns>
 public static bool IsNullableType(TypeInfo typeInfo) => TypeJudgment.IsNullableType(typeInfo);
示例#9
0
 /// <summary>
 /// Is nullable type
 /// </summary>
 /// <param name="type"></param>
 /// <returns></returns>
 public static bool IsNullableType(Type type) => TypeJudgment.IsNullableType(type);
示例#10
0
 /// <summary>
 /// Is nullable type
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <returns></returns>
 public static bool IsNullableType <T>() => TypeJudgment.IsNullableType <T>();
示例#11
0
 public static bool IsNullableGuidType(Type type)
 {
     return(TypeJudgment.IsNullableType(type) && IsGuidType(Nullable.GetUnderlyingType(type)));
 }