Esempio n. 1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="CollectionItems"/> class.
        /// </summary>
        /// <param name="type">The type.</param>
        public CollectionItems(Type type, string name, Func <object, object> getter, Action <object, object> setter, ITypeResolver typeResolver)
        {
            this.type = type;
            if (name != null)
            {
                this.Name = name;
            }
            else
            {
                this.Name = type.FullName;
            }
            this.isArray = type.IsArray;
            this.setter  = setter;
            this.getter  = getter;
            this.typeId  = CalculateTypeId();

            if (isArray)
            {
                itemType    = type.GetElementType();
                isByteArray = itemType.Equals(typeof(byte));
            }
            else
            {
                // type is a collection
                // check if collection is generic IEnumerable<T>
                if (type.IsGenericType)
                {
                    Type   genericCollectionInterface = type.GetInterface("IEnumerable`1");
                    Type[] genericTypes = type.GetGenericArguments();
                    if (genericTypes.Length == 1)
                    {
                        Type listType = typeof(List <>);
                        targetCollectionType = listType.MakeGenericType(genericTypes);
                        itemType             = genericTypes[0];
                    }
                    else if (genericTypes.Length == 0 &&
                             genericCollectionInterface != null)
                    {
                        genericTypes = genericCollectionInterface.GetGenericArguments();
                        Type listType = typeof(List <>);
                        targetCollectionType = listType.MakeGenericType(genericTypes);
                        itemType             = genericTypes[0];
                    }
                    else
                    {
                        throw new NotImplementedException("More than one generic arguments is not supported yet!");
                    }
                }
                else
                {
                    var defaultConstructor = type.GetConstructor(System.Type.EmptyTypes);

                    if (defaultConstructor != null)
                    {
                        targetCollectionType = type;
                    }
                    else
                    {
                        // untyped collection
                        targetCollectionType = typeof(ArrayList);
                    }
                    itemType = typeof(object);
                }

                implementsIList = targetCollectionType.GetInterface("IList") != null;
                if (!implementsIList)
                {
                    specialCollAddMethod = targetCollectionType.GetMethod("Add");
                }
            }

            itemStructure = typeResolver.GetByType(itemType);
        }