コード例 #1
0
ファイル: Reflection.cs プロジェクト: josheinhorn/dd4t-2-net
        public IModelProperty GetModelProperty(PropertyInfo propertyInfo, IPropertyAttribute attribute)
        {
            if (propertyInfo == null)
            {
                throw new ArgumentNullException("propertyInfo");
            }
            //if (attribute == null) throw new ArgumentNullException("attribute");
            var  modelType = propertyInfo.PropertyType;
            Type elementType;
            bool isArray      = false;
            bool isCollection = false;
            bool isEnumerable = false;
            Action <object, object>   addToCollection = null;
            Func <IEnumerable, Array> toArray         = null;

            if (isArray = helper.IsArray(modelType, out elementType)) //Array is ICollection<> and IEnumerable
            {
                modelType = elementType;
                toArray   = helper.BuildToArray(elementType);
            }
            else if (isCollection = helper.IsGenericCollection(modelType, out elementType)) //ICollection<> is IEnumerable
            {
                addToCollection = helper.BuildAddMethod(modelType);
                modelType       = elementType;
            }
            else
            {
                isEnumerable = helper.IsEnumerable(propertyInfo.PropertyType);  //Fallback to plain IEnumerable
            }
            return(new ModelProperty
            {
                Name = propertyInfo.Name,
                PropertyAttribute = attribute,
                Set = helper.BuildSetter(propertyInfo),
                Get = helper.BuildGetter(propertyInfo),
                PropertyType = propertyInfo.PropertyType,
                IsEnumerable = isEnumerable,
                IsCollection = isCollection,
                IsArray = isArray,
                ModelType = modelType,
                AddToCollection = addToCollection,
                ToArray = toArray
            });
        }