Ejemplo n.º 1
0
        private static object GetSitecoreItemFieldValue(Type propertyType, Item item, SitecoreItemFieldAttribute attribute, bool isLazyLoad)
        {
            if (propertyType == _types[DataTypes.String])
            {
                return(item.GetString(attribute.FieldId));
            }
            if (propertyType == _types[DataTypes.DateTime])
            {
                return(item.GetDateTime(attribute.FieldId));
            }
            if (propertyType == _types[DataTypes.Int])
            {
                return(item.GetInt(attribute.FieldId));
            }
            if (propertyType == _types[DataTypes.Double])
            {
                return(item.GetDouble(attribute.FieldId));
            }
            if (propertyType == _types[DataTypes.Bool])
            {
                return(item.GetBool(attribute.FieldId));
            }
            if (propertyType == _types[DataTypes.Item])
            {
                return(item.GetItem(attribute.FieldId));
            }
            if (propertyType == _types[DataTypes.ItemEnumerable])
            {
                return(item.GetItems(attribute.FieldId));
            }
            if (propertyType == _types[DataTypes.CustomField] || propertyType.IsSubclassOf(_types[DataTypes.CustomField]))
            {
                Field field = item.Fields[attribute.FieldId];
                if (field != null)
                {
                    return(ReflectionUtil.CreateObject(propertyType, new object[] { field }));
                }
            }
            // got to figure out a better way to do this
            if (propertyType == _types[DataTypes.SitecoreItem] || propertyType.IsSubclassOf(_types[DataTypes.SitecoreItem]))
            {
                Item fieldValueItem = item.GetItem(attribute.FieldId);
                if (fieldValueItem != null && ItemExtensions.IsItemValidForType(propertyType, fieldValueItem))
                {
                    return(TypeMapper.CreateObject(item.GetItem(attribute.FieldId), propertyType, isLazyLoad));
                }
            }
            // got to figure out a better way to do this

            if (_types[DataTypes.SitecoreItemEnumerable].IsAssignableFrom(propertyType))
            {
                Type[] types = propertyType.GetGenericArguments();
                if (!types.Any())
                {
                    throw new Exception("XCore needs at least one type argument");
                }
                if (types.Count() > 1)
                {
                    throw new Exception("XCore only supports one type argument");
                }
                Type  classType = types[0];
                Type  listType  = typeof(List <>).MakeGenericType(classType);
                IList list      = Activator.CreateInstance(listType) as IList;
                if (list == null)
                {
                    throw new Exception("XCore could not create a list of object type" + listType);
                }
                foreach (Item itm in item.GetItems(attribute.FieldId))
                {
                    if (itm == null || !ItemExtensions.IsItemValidForType(classType, itm))
                    {
                        continue;
                    }
                    object typeObject = TypeMapper.CreateObject(itm, classType, isLazyLoad);
                    if (typeObject != null)
                    {
                        list.Add(typeObject);
                    }
                }
                return(list);
            }
            return(null);
        }