private static Expression GetCollectionCreation(EnumerableTypeHelper typeHelper, Expression list) { // ReSharper disable once AssignNullToNotNullAttribute return(Expression.New( typeHelper.CollectionType.GetPublicInstanceConstructor(typeHelper.ListInterfaceType), list)); }
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())); }
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)); }
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)); }
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)); }
protected static Expression GetLinqMethodCall( string methodName, Expression enumerable, EnumerableTypeHelper helper) { return(Expression.Call( typeof(Enumerable) .GetPublicStaticMethod(methodName, parameterCount: 1) .MakeGenericMethod(helper.ElementType), enumerable)); }
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)); }
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)); }
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)); }
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); }
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)); }
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); }
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)); }
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)); }
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)); }
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); }
private static MethodInfo GetNonListToArrayConversionMethod(EnumerableTypeHelper typeHelper) => typeHelper.HasCollectionInterface ? _collectionToArrayMethod : _linqToArrayMethod;
private static Expression GetReadOnlyCollectionCreation(EnumerableTypeHelper typeHelper, Expression list) => GetCopyListCollectionCreation(typeHelper, typeHelper.ReadOnlyCollectionType, list);