private static List <Expression> BuildExtractionOfChildModelSetter([NotNull] Type currNodeType, [NotNull] Expression currNodeExpression, [NotNull] Expression valueToSetExpression, [NotNull, ItemNotNull] string[] pathParts)
        {
            var statements = new List <Expression>();

            for (var partIndex = 0; partIndex < pathParts.Length; ++partIndex)
            {
                var name = TemplateDescriptionHelper.GetPathPartName(pathParts[partIndex]);

                var newNodeType = currNodeType.GetProperty(name)?.PropertyType;
                currNodeType       = newNodeType ?? throw new ObjectPropertyExtractionException($"Type '{currNodeType}' has no property '{name}'");
                currNodeExpression = Expression.Property(currNodeExpression, name);

                if (TemplateDescriptionHelper.IsCollectionAccessPathPart(pathParts[partIndex]))
                {
                    List <Expression> statementsToAdd;
                    (currNodeExpression, currNodeType, statementsToAdd) = BuildExpandingOfCollectionAccessPart(currNodeExpression, currNodeType, pathParts[partIndex]);
                    statements.AddRange(statementsToAdd);
                }
                else if (TemplateDescriptionHelper.IsArrayPathPart(pathParts[partIndex]))
                {
                    var statementsToAdd = BuildExpandingOfArrayPart(currNodeExpression, currNodeType, valueToSetExpression, pathParts.Skip(partIndex + 1).ToArray());
                    statements.AddRange(statementsToAdd);
                    return(statements);
                }
                else if (!TypeCheckingHelper.IsNullable(currNodeType) && partIndex != pathParts.Length - 1)
                {
                    statements.Add(ExpressionPrimitives.CreateValueInitStatement(currNodeExpression, currNodeType));
                }
            }
            statements.Add(ExpressionPrimitives.AssignWithTypeCheckings(currNodeExpression, currNodeType, valueToSetExpression));
            return(statements);
        }
Example #2
0
        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);
        }
Example #3
0
 private static bool TryExtractDirectChild([NotNull] object model, [NotNull] string pathPart, out object child)
 {
     child = null;
     if (TemplateDescriptionHelper.IsCollectionAccessPathPart(pathPart))
     {
         var name = TemplateDescriptionHelper.GetCollectionAccessPathPartName(pathPart);
         var key  = TemplateDescriptionHelper.GetCollectionAccessPathPartIndex(pathPart);
         if (!TryExtractCurrentChildPropertyInfo(model, name, out var collectionPropertyInfo))
         {
             return(false);
         }
         if (TypeCheckingHelper.IsDictionary(collectionPropertyInfo.PropertyType))
         {
             var indexer = TemplateDescriptionHelper.ParseCollectionIndexerOrThrow(key, TypeCheckingHelper.GetDictionaryGenericTypeArguments(collectionPropertyInfo.PropertyType).keyType);
             var dict    = collectionPropertyInfo.GetValue(model, null);
             if (dict == null)
             {
                 return(true);
             }
             child = ((IDictionary)dict)[indexer];
             return(true);
         }
         if (TypeCheckingHelper.IsIList(collectionPropertyInfo.PropertyType))
         {
             var indexer = (int)TemplateDescriptionHelper.ParseCollectionIndexerOrThrow(key, typeof(int));
             var list    = collectionPropertyInfo.GetValue(model, null);
             if (list == null)
             {
                 return(true);
             }
             child = ((IList)list)[indexer];
             return(true);
         }
         throw new ObjectPropertyExtractionException($"Unexpected child type: expected dictionary or array (pathPath='{pathPart}'), but model is '{collectionPropertyInfo.PropertyType}' in '{model.GetType()}'");
     }
     if (!TryExtractCurrentChildPropertyInfo(model, pathPart, out var propertyInfo))
     {
         return(false);
     }
     child = propertyInfo.GetValue(model, null);
     return(true);
 }