private static List <Expression> BuildExpandingOfArrayPart([NotNull] Expression currNodeExpression, [NotNull] Type currNodeType, [NotNull] Expression valueToSetExpression, [NotNull, ItemNotNull] string[] pathParts) { var statements = new List <Expression>(); if (currNodeType.IsArray) { var itemType = TypeCheckingHelper.GetEnumerableItemType(currNodeType); var getLenExpression = Expression.Property(Expression.Convert(valueToSetExpression, typeof(ICollection)), "Count"); statements.Add(ExpressionPrimitives.CreateArrayInitStatement(currNodeExpression, itemType, getLenExpression)); var expressionLoopVar = Expression.Variable(typeof(int)); var elementExpression = Expression.ArrayAccess(currNodeExpression, expressionLoopVar); var elementInitStatement = ExpressionPrimitives.CreateValueInitStatement(elementExpression, itemType); var setItemStatements = Expression.Block(BuildExtractionOfChildModelSetter(itemType, elementExpression, ExpressionPrimitives.GetIndexAccessExpression(valueToSetExpression, expressionLoopVar), pathParts)); var loopBodyExpression = Expression.Block(elementInitStatement, setItemStatements); var loopExpression = ExpressionPrimitives.ForFromTo(expressionLoopVar, Expression.Constant(0), getLenExpression, loopBodyExpression); statements.Add(loopExpression); return(statements); } throw new ObjectPropertyExtractionException("Only array is supported as iterated collection"); }
public static Type ExtractChildObjectTypeFromPath([NotNull] Type modelType, [NotNull] ExcelTemplatePath path) { var currType = modelType; foreach (var part in path.PartsWithIndexers) { var childPropertyType = ExtractPropertyInfo(currType, part).PropertyType; if (TemplateDescriptionHelper.IsCollectionAccessPathPart(part)) { if (TypeCheckingHelper.IsDictionary(childPropertyType)) { var(keyType, valueType) = TypeCheckingHelper.GetDictionaryGenericTypeArguments(childPropertyType); TemplateDescriptionHelper.ParseCollectionIndexerOrThrow(TemplateDescriptionHelper.GetCollectionAccessPathPartIndex(part), keyType); currType = valueType; } else if (TypeCheckingHelper.IsIList(childPropertyType)) { TemplateDescriptionHelper.ParseCollectionIndexerOrThrow(TemplateDescriptionHelper.GetCollectionAccessPathPartIndex(part), typeof(int)); currType = TypeCheckingHelper.GetEnumerableItemType(childPropertyType); } else { throw new ObjectPropertyExtractionException($"Not supported collection type {childPropertyType}"); } } else if (TemplateDescriptionHelper.IsArrayPathPart(part)) { if (TypeCheckingHelper.IsIList(childPropertyType)) { currType = TypeCheckingHelper.GetIListItemType(childPropertyType); } else { throw new ObjectPropertyExtractionException($"Not supported collection type {childPropertyType}"); } } else { currType = childPropertyType; } } return(currType); }