public static Expression GetExpression(Type openConstructorType, MapMode mapMode, Type targetCollType, Type sourceCollType, Expression listExpr, Expression includeChain, List <Tuple <Type, Type> > usedBuilders) { Type sourceType = sourceCollType.GetElementTypeOrType(); Type targetType = targetCollType.GetElementTypeOrType(); var closedConstructorType = openConstructorType.MakeGenericType(targetType); var resultExpr = Expression.Variable(targetCollType); var itemExpr = Expression.Variable(sourceType); var itemMapExpression = PolymorphismManager.GetMostConcreteExpressionCreator(mapMode, itemExpr, targetType, includeChain, usedBuilders); if (itemMapExpression == null) { return(null); } var loopContent = Expression.Block( Expression.Call( resultExpr, closedConstructorType.GetTypeInfo().GetMethod("Add"), itemMapExpression ) ); var result = Expression.Block( new ParameterExpression[] { resultExpr, itemExpr }, Expression.Assign(resultExpr, Expression.New(closedConstructorType)), loopContent.ForEach( listExpr, itemExpr ), resultExpr ); return(result); }
/// <summary> /// Get the expression that returns the mapped array. /// </summary> /// <param name="mapMode"></param> /// <param name="targetCollType"></param> /// <param name="sourceCollType"></param> /// <param name="listExpr"></param> /// <param name="includeChain"></param> /// <param name="usedBuilders"></param> /// <returns></returns> public static Expression GetExpression(MapMode mapMode, Type targetCollType, Type sourceCollType, Expression listExpr, Expression includeChain, List <Tuple <Type, Type> > usedBuilders) { Type targetType = targetCollType.GetElementTypeOrType(); Type sourceType = sourceCollType.GetElementTypeOrType(); ParameterExpression varSource = Expression.Variable(sourceType); ParameterExpression varObjectsArray = Expression.Variable(sourceType.MakeArrayType()); ParameterExpression varResult = Expression.Variable(targetType.MakeArrayType()); ParameterExpression varIndex = Expression.Parameter(typeof(int)); var buildExpression = PolymorphismManager.GetMostConcreteExpressionCreator(mapMode, varSource, targetType, includeChain, usedBuilders); if (buildExpression == null) { return(null); } Expression sourceAsArrayExpr = Expression.Call( Meta.Method(() => Enumerable.ToArray <object>(null)).GetGenericMethodDefinition().MakeGenericMethod(sourceType), listExpr ); Expression loopContent = Expression.Block( Expression.Assign(varSource, Expression.ArrayAccess(varObjectsArray, varIndex)), Expression.Assign( Expression.ArrayAccess(varResult, varIndex), buildExpression.Convert(targetType) ) ); return(Expression.Block( new ParameterExpression[] { varSource, varResult, varObjectsArray }, Expression.Assign(varObjectsArray, sourceAsArrayExpr), Expression.Assign( varResult, Expression.NewArrayBounds(targetType, Expression.Property(varObjectsArray, Meta <Array> .Property(x => x.Length))) ), loopContent.For( varIndex, Expression.Assign(varIndex, Expression.Constant(0)), Expression.LessThan(varIndex, Expression.Property(varObjectsArray, Meta <Array> .Property(x => x.Length))), Expression.Assign(varIndex, Expression.Increment(varIndex)) ), varResult )); }
/// <summary> /// Returns the expression that returns the mapped dictionary. /// </summary> /// <param name="mapMode"></param> /// <param name="targetCollType"></param> /// <param name="sourceCollType"></param> /// <param name="source"></param> /// <param name="includeChain"></param> /// <param name="usedBuilders"></param> /// <returns></returns> public static Expression GetExpression(MapMode mapMode, Type targetCollType, Type sourceCollType, Expression source, Expression includeChain, List <Tuple <Type, Type> > usedBuilders) { Type sourceType = sourceCollType.GetElementTypeOrType(); // KeyValuePair Type targetType = targetCollType.GetElementTypeOrType(); // KeyValuePair var targetArgTypes = targetType.GetTypeInfo().GetGenericArguments(); // args of KeyValuePair var sourceArgTypes = sourceType.GetTypeInfo().GetGenericArguments(); // args of KeyValuePair var dicType = typeof(Dictionary <,>).MakeGenericType(targetArgTypes[0], targetArgTypes[1]); var dicResultExpr = Expression.Variable(targetCollType); var kpExpr = Expression.Variable(typeof(KeyValuePair <,>).MakeGenericType(sourceArgTypes[0], sourceArgTypes[1])); var keyMapExpression = PolymorphismManager.GetMostConcreteExpressionCreator(mapMode, Expression.Property(kpExpr, "Key"), targetArgTypes[0], includeChain, usedBuilders); var valueMapExpression = PolymorphismManager.GetMostConcreteExpressionCreator(mapMode, Expression.Property(kpExpr, "Value"), targetArgTypes[1], includeChain, usedBuilders); if (keyMapExpression == null || valueMapExpression == null) { return(null); } var loopContent = Expression.Block( Expression.Call( dicResultExpr, dicType.GetMethod("Add"), keyMapExpression, valueMapExpression ) ); return(Expression.Block( new ParameterExpression[] { dicResultExpr, kpExpr }, Expression.Assign(dicResultExpr, Expression.New(dicType)), loopContent.ForEach( source, kpExpr ), dicResultExpr )); }