Exemplo n.º 1
0
        public static IList CreateAndPopulateList(Type listType, Action <IList, bool> populateList)
        {
            ValidationUtils.ArgumentNotNull(listType, "listType");
            ValidationUtils.ArgumentNotNull(populateList, "populateList");

            IList list;
            Type  collectionType;
            bool  isReadOnlyOrFixedSize = false;

            if (listType.IsArray)
            {
                // have to use an arraylist when creating array
                // there is no way to know the size until it is finised
                list = new List <object>();
                isReadOnlyOrFixedSize = true;
            }
            else if (ReflectionUtils.InheritsGenericDefinition(listType, typeof(ReadOnlyCollection <>), out collectionType))
            {
                Type readOnlyCollectionContentsType = collectionType.GetGenericArguments()[0];
                Type genericEnumerable   = ReflectionUtils.MakeGenericType(typeof(IEnumerable <>), readOnlyCollectionContentsType);
                bool suitableConstructor = false;

                foreach (ConstructorInfo constructor in listType.GetConstructors())
                {
                    IList <ParameterInfo> parameters = constructor.GetParameters();

                    if (parameters.Count == 1)
                    {
                        if (genericEnumerable.IsAssignableFrom(parameters[0].ParameterType))
                        {
                            suitableConstructor = true;
                            break;
                        }
                    }
                }

                if (!suitableConstructor)
                {
                    throw new Exception("Read-only type {0} does not have a public constructor that takes a type that implements {1}.".FormatWith(CultureInfo.InvariantCulture, listType, genericEnumerable));
                }

                // can't add or modify a readonly list
                // use List<T> and convert once populated
                list = (IList)CreateGenericList(readOnlyCollectionContentsType);
                isReadOnlyOrFixedSize = true;
            }
            else if (typeof(IList).IsAssignableFrom(listType))
            {
                if (ReflectionUtils.IsInstantiatableType(listType))
                {
                    list = (IList)Activator.CreateInstance(listType);
                }
                else if (listType == typeof(IList))
                {
                    list = new List <object>();
                }
                else
                {
                    list = null;
                }
            }
            else if (listType.IsGenericType && listType.GetGenericTypeDefinition() == typeof(IList <>))
            {
                list = CollectionUtils.CreateGenericList(ReflectionUtils.GetCollectionItemType(listType));
            }
            else
            {
                list = null;
            }

            if (list == null)
            {
                throw new Exception("Cannot create and populate list type {0}.".FormatWith(CultureInfo.InvariantCulture, listType));
            }

            populateList(list, isReadOnlyOrFixedSize);

            // create readonly and fixed sized collections using the temporary list
            if (isReadOnlyOrFixedSize)
            {
                if (listType.IsArray)
                {
                    list = ToArray(((List <object>)list).ToArray(), ReflectionUtils.GetCollectionItemType(listType));
                }
                else if (ReflectionUtils.InheritsGenericDefinition(listType, typeof(ReadOnlyCollection <>)))
                {
                    list = (IList)ReflectionUtils.CreateInstance(listType, list);
                }
            }

            return(list);
        }
Exemplo n.º 2
0
        public static IList CreateGenericList(Type listType)
        {
            ValidationUtils.ArgumentNotNull(listType, "listType");

            return((IList)ReflectionUtils.CreateGeneric(typeof(List <>), listType));
        }