コード例 #1
0
        public object As(Type type)
        {
            var sitecoreTemplateAttributeInfo = SitecoreTemplateAttributesCache.TryToGetInfoForAType(type);

            if (!IsValidTemplate(sitecoreTemplateAttributeInfo.SitecoreTemplateAttribute, this.TemplateId, this.TemplateName))
            {
                return(null);
            }

            return(ConvertItemToModel(this, sitecoreTemplateAttributeInfo));
        }
コード例 #2
0
        private static IList ConvertChildren(Type childType, IList <ISitecoreItem> children)
        {
            var list = (IList)Activator.CreateInstance(typeof(List <>).MakeGenericType(childType));

            if (!children.Any())
            {
                return(list);
            }

            var childrenGroups = children.GroupBy(
                x => new { x.TemplateId, x.TemplateName },
                x => x,
                (key, items) => new { Template = key, Items = items.ToList() });

            var childSitecoreTemplateAttributeInfos = GetTemplateInheritanceList(
                SitecoreTemplateAttributesCache.TryToGetInfoForAType(childType));

            foreach (var childSitecoreTemplateAttributeInfo in childSitecoreTemplateAttributeInfos)
            {
                var childrenGroup = childrenGroups.FirstOrDefault(x =>
                                                                  IsValidTemplate(
                                                                      childSitecoreTemplateAttributeInfo.SitecoreTemplateAttribute,
                                                                      x.Template.TemplateId,
                                                                      x.Template.TemplateName));

                if (childrenGroup == null)
                {
                    continue;
                }

                foreach (var obj in childrenGroup.Items
                         .Select(x => ConvertItemToModel(x, childSitecoreTemplateAttributeInfo)))
                {
                    list.Add(obj);
                }
            }

            return(list);
        }