Пример #1
0
 private static Expression GetCollectionCreation(EnumerableTypeHelper typeHelper, Expression list)
 {
     // ReSharper disable once AssignNullToNotNullAttribute
     return(Expression.New(
                typeHelper.CollectionType.GetPublicInstanceConstructor(typeHelper.ListInterfaceType),
                list));
 }
Пример #2
0
        public static Expression GetEmptyInstanceCreation(
            this Type enumerableType,
            Type elementType,
            EnumerableTypeHelper typeHelper = null)
        {
            if (enumerableType.IsArray)
            {
                return(GetEmptyArray(elementType));
            }

            if (enumerableType.IsValueType())
            {
                return(Expression.New(enumerableType));
            }

            if (typeHelper == null)
            {
                typeHelper = new EnumerableTypeHelper(enumerableType, elementType);
            }

            if (typeHelper.IsEnumerableInterface)
            {
                return(Expression.Field(null, typeof(Enumerable <>).MakeGenericType(elementType), "Empty"));
            }

            if (typeHelper.IsReadOnlyCollection)
            {
                return(GetReadOnlyCollectionCreation(typeHelper, GetEmptyArray(elementType)));
            }

            return(Expression.New(typeHelper.GetEmptyInstanceCreationFallbackType()));
        }
Пример #3
0
            private static Expression GetHasEnumerableCheck(Expression enumerableAccess)
            {
                var helper = new EnumerableTypeHelper(enumerableAccess.Type);

                return(helper.IsEnumerableInterface
                    ? GetLinqMethodCall(nameof(Enumerable.Any), enumerableAccess, helper)
                    : GetEnumerableCountCheck(enumerableAccess, helper));
            }
            private static Expression GetHasEnumerableCheck(Expression enumerableAccess)
            {
                var helper = new EnumerableTypeHelper(enumerableAccess.Type);

                return(helper.IsEnumerableOrQueryable
                    ? GetLinqMethodCall(nameof(Enumerable.Any), enumerableAccess, helper)
                    : helper.GetNonZeroCountCheck(enumerableAccess));
            }
Пример #5
0
            public override Expression GetAccess(Expression enumerableAccess)
            {
                var helper = new EnumerableTypeHelper(enumerableAccess.Type);

                var count = helper.IsEnumerableInterface
                    ? GetLinqMethodCall(nameof(Enumerable.Count), enumerableAccess, helper)
                    : helper.GetCountFor(enumerableAccess, MapperData.TargetMember.Type.GetNonNullableType());

                return(count.GetConversionTo(MapperData.TargetMember.Type));
            }
Пример #6
0
        private static Expression GetCopyListCollectionCreation(
            EnumerableTypeHelper typeHelper,
            Type collectonType,
            Expression list)
        {
            var copyListConstructor = collectonType.GetPublicInstanceConstructor(typeHelper.ListInterfaceType);

            return((copyListConstructor != null)
                ? Expression.New(copyListConstructor, list)
                : Expression.New(collectonType));
        }
Пример #7
0
 protected static Expression GetLinqMethodCall(
     string methodName,
     Expression enumerable,
     EnumerableTypeHelper helper)
 {
     return(Expression.Call(
                typeof(Enumerable)
                .GetPublicStaticMethod(methodName, parameterCount: 1)
                .MakeGenericMethod(helper.ElementType),
                enumerable));
 }
Пример #8
0
            private Expression GetCondition(Expression enumerableAccess, EnumerableTypeHelper helper)
            {
                var enumerableCheck = HasMetaMemberPart.GetEnumerableCountCheck(enumerableAccess, helper);

                if (MapperData.RuleSet.Settings.GuardAccessTo(enumerableAccess))
                {
                    enumerableCheck = Expression.AndAlso(
                        enumerableAccess.GetIsNotDefaultComparison(),
                        enumerableCheck);
                }

                return(enumerableCheck);
            }
            protected static Expression GetLinqMethodCall(
                string methodName,
                Expression enumerable,
                EnumerableTypeHelper helper)
            {
                var enumerableType = helper.IsQueryableInterface ? typeof(Queryable) : typeof(Enumerable);

                return(Expression.Call(
                           enumerableType
                           .GetPublicStaticMethod(methodName, parameterCount: 1)
                           .MakeGenericMethod(helper.ElementType),
                           enumerable));
            }
Пример #10
0
        public static Expression GetCollectionTypeCreation(this Expression enumerable, Type elementType)
        {
            var typeHelper = new EnumerableTypeHelper(enumerable.Type, elementType);

            if (typeHelper.HasListInterface)
            {
                return(GetCollectionTypeCreation(typeHelper, enumerable.GetConversionTo(typeHelper.ListInterfaceType)));
            }

            var nonListToArrayMethod = GetNonListToArrayConversionMethod(typeHelper);
            var toArrayCall          = GetToEnumerableCall(enumerable, nonListToArrayMethod, typeHelper.ElementType);

            return(GetCollectionTypeCreation(typeHelper, toArrayCall));
        }
Пример #11
0
        private static MethodInfo GetToArrayConversionMethod(Expression enumerable, Type elementType)
        {
            var typeHelper = new EnumerableTypeHelper(enumerable.Type, elementType);

            if (TryGetWrapperMethod(typeHelper, "ToArray", out var method))
            {
                return(method);
            }

            if (typeHelper.HasListInterface)
            {
                return(_listToArrayMethod);
            }

            return(GetNonListToArrayConversionMethod(typeHelper));
        }
Пример #12
0
        private static bool TryGetWrapperMethod(
            EnumerableTypeHelper typeHelper,
            string methodName,
            out MethodInfo method)
        {
            var wrapperType = typeHelper.WrapperType;

            if (typeHelper.EnumerableType != wrapperType)
            {
                method = null;
                return(false);
            }

            method = wrapperType.GetPublicInstanceMethod(methodName);
            return(true);
        }
Пример #13
0
        public static Expression GetEmptyInstanceCreation(this Type enumerableType, Type elementType = null)
        {
            if (elementType == null)
            {
                elementType = enumerableType.GetEnumerableElementType();
            }

            if (enumerableType.IsArray)
            {
                return(Expression.Field(null, _typedEnumerable.MakeGenericType(elementType), "EmptyArray"));
            }

            var typeHelper = new EnumerableTypeHelper(enumerableType, elementType);

            if (typeHelper.IsEnumerableInterface)
            {
                return(Expression.Field(null, _typedEnumerable.MakeGenericType(elementType), "Empty"));
            }

            return(Expression.New(typeHelper.IsCollection ? typeHelper.CollectionType : typeHelper.ListType));
        }
Пример #14
0
            public override Expression GetAccess(Expression parentInstance)
            {
                var enumerableAccess = NextPart.GetAccess(parentInstance);

                var helper = new EnumerableTypeHelper(enumerableAccess.Type, _sourceMember.Type);

                _condition = GetCondition(enumerableAccess, helper);

                var valueAccess = (helper.HasListInterface && MapperData.RuleSet.Settings.AllowIndexAccesses)
                    ? enumerableAccess.GetIndexAccess(GetIndex(enumerableAccess))
                    : GetOrderedEnumerableAccess(enumerableAccess, helper);

                if (NextPart.NextPart != null)
                {
                    valueAccess = NextPart.NextPart.GetAccess(valueAccess);
                }

                if (MapperData.TargetMember.IsSimple)
                {
                    return(MapperData.GetValueConversion(valueAccess, MapperData.TargetMember.Type));
                }

                return(valueAccess);
            }
Пример #15
0
        public static Expression GetReadOnlyCollectionCreation(this Expression enumerable, Type elementType)
        {
            var typeHelper = new EnumerableTypeHelper(enumerable.Type, elementType);

            if (TryGetWrapperMethod(typeHelper, "ToReadOnlyCollection", out var method))
            {
                return(GetToEnumerableCall(enumerable, method, typeHelper.ElementType));
            }

            if (typeHelper.IsList)
            {
                return(Expression.Call(enumerable, typeHelper.ListType.GetPublicInstanceMethod("AsReadOnly")));
            }

            if (typeHelper.HasListInterface)
            {
                return(GetReadOnlyCollectionCreation(typeHelper, enumerable));
            }

            var nonListToArrayMethod = GetNonListToArrayConversionMethod(typeHelper);
            var toArrayCall          = GetToEnumerableCall(enumerable, nonListToArrayMethod, typeHelper.ElementType);

            return(GetReadOnlyCollectionCreation(typeHelper, toArrayCall));
        }
Пример #16
0
            private Expression GetOrderedEnumerableAccess(Expression enumerableAccess, EnumerableTypeHelper helper)
            {
                var elementType = _sourceMember.Type;

                if (!elementType.IsComplex())
                {
                    return(GetLinqMethodCall(LinqSelectionMethodName, enumerableAccess, helper));
                }

                var orderMember =
                    elementType.GetPublicInstanceMember("Order") ??
                    elementType.GetPublicInstanceMember("DateCreated") ??
                    MapperData.MapperContext.Naming.GetIdentifierOrNull(elementType)?.MemberInfo;

                if (orderMember == null)
                {
                    return(GetLinqMethodCall(LinqSelectionMethodName, enumerableAccess, helper));
                }

                var element = Parameters.Create(_sourceMember.Type);

                enumerableAccess = enumerableAccess.WithOrderingLinqCall(
                    LinqOrderingMethodName,
                    element,
                    Expression.MakeMemberAccess(element, orderMember));

                return(GetLinqMethodCall(nameof(Enumerable.FirstOrDefault), enumerableAccess, helper));
            }
Пример #17
0
            private Expression GetOrderedEnumerableAccess(Expression enumerableAccess, EnumerableTypeHelper helper)
            {
                var elementType = _sourceMember.Type;

                if (!elementType.IsComplex())
                {
                    return(GetLinqMethodCall(LinqSelectionMethodName, enumerableAccess, helper));
                }

                var orderMember = MapperData.GetOrderMember(elementType);

                if (orderMember == null)
                {
                    return(GetLinqMethodCall(LinqSelectionMethodName, enumerableAccess, helper));
                }

                var element = Parameters.Create(_sourceMember.Type);

                enumerableAccess = enumerableAccess.WithOrderingLinqCall(
                    LinqOrderingMethodName,
                    element,
                    Expression.MakeMemberAccess(element, orderMember));

                return(GetLinqMethodCall(nameof(Enumerable.FirstOrDefault), enumerableAccess, helper));
            }
Пример #18
0
            public static Expression GetEnumerableCountCheck(Expression enumerableAccess, EnumerableTypeHelper helper)
            {
                var enumerableCount = helper.GetCountFor(enumerableAccess);
                var zero            = ToNumericConverter <int> .Zero.GetConversionTo(enumerableCount.Type);

                var countGreaterThanZero = Expression.GreaterThan(enumerableCount, zero);

                return(countGreaterThanZero);
            }
Пример #19
0
 private static MethodInfo GetNonListToArrayConversionMethod(EnumerableTypeHelper typeHelper)
 => typeHelper.HasCollectionInterface ? _collectionToArrayMethod : _linqToArrayMethod;
Пример #20
0
 private static Expression GetReadOnlyCollectionCreation(EnumerableTypeHelper typeHelper, Expression list)
 => GetCopyListCollectionCreation(typeHelper, typeHelper.ReadOnlyCollectionType, list);