Ejemplo n.º 1
0
        public static object ExtractChildObject([NotNull] object model, [NotNull] ExcelTemplatePath path)
        {
            var pathParts = path.PartsWithIndexers;

            var(succeed, result) = TryExtractChildObject(model, pathParts, 0);
            if (!succeed)
            {
                throw new ObjectPropertyExtractionException($"Can't find path '{string.Join(".", pathParts)}' in model of type '{model.GetType()}'");
            }

            return(result);
        }
        public (ExcelTemplatePath pathToEnumerable, ExcelTemplatePath relativePathToItem) SplitForEnumerableExpansion()
        {
            if (!HasArrayAccess)
            {
                throw new BaseExcelSerializationException($"Expression needs enumerable expansion but has no part with '[]' or '[#]' (path - '{RawPath}')");
            }
            var pathToEnumerableLength = PartsWithIndexers.TakeWhile(x => !TemplateDescriptionHelper.IsArrayPathPart(x)).Count() + 1;
            var pathToEnumerable       = new ExcelTemplatePath(string.Join(".", PartsWithIndexers.Take(pathToEnumerableLength)));
            var relativePathToItem     = new ExcelTemplatePath(string.Join(".", PartsWithIndexers.Skip(pathToEnumerableLength)));

            return(pathToEnumerable, relativePathToItem);
        }
Ejemplo n.º 3
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);
        }
        public static Action <object> ExtractChildObjectSetter([NotNull] object model, [NotNull] ExcelTemplatePath path)
        {
            var action = childObjectSettersCache.GetOrAdd((model.GetType(), path), x => ExtractChildModelSetter(x.type, x.path.PartsWithIndexers));

            return(x => action(model, x));
        }
 private bool Equals(ExcelTemplatePath other)
 {
     return(string.Equals(RawPath, other.RawPath));
 }