Exemplo n.º 1
0
        public static IList CreateList(Type listType, out bool isReadOnlyOrFixedSize)
        {
            ValidationUtils.ArgumentNotNull((object)listType, "listType");
            isReadOnlyOrFixedSize = false;
            IList list1;

            if (listType.IsArray)
            {
                list1 = (IList) new List <object>();
                isReadOnlyOrFixedSize = true;
            }
            else
            {
                Type implementingType;
                if (ReflectionUtils.InheritsGenericDefinition(listType, typeof(ReadOnlyCollection <>), out implementingType))
                {
                    Type listType1 = implementingType.GetGenericArguments()[0];
                    Type type      = ReflectionUtils.MakeGenericType(typeof(IEnumerable <>), new Type[1]
                    {
                        listType1
                    });
                    bool flag = false;
                    foreach (MethodBase methodBase in listType.GetConstructors())
                    {
                        IList <ParameterInfo> list2 = (IList <ParameterInfo>)methodBase.GetParameters();
                        if (list2.Count == 1 && type.IsAssignableFrom(list2[0].ParameterType))
                        {
                            flag = true;
                            break;
                        }
                    }
                    if (!flag)
                    {
                        throw new Exception(StringUtils.FormatWith("Read-only type {0} does not have a public constructor that takes a type that implements {1}.", (IFormatProvider)CultureInfo.InvariantCulture, (object)listType, (object)type));
                    }
                    list1 = CollectionUtils.CreateGenericList(listType1);
                    isReadOnlyOrFixedSize = true;
                }
                else
                {
                    list1 = !typeof(IList).IsAssignableFrom(listType) ? (!ReflectionUtils.ImplementsGenericDefinition(listType, typeof(ICollection <>)) ? (IList)null : (!ReflectionUtils.IsInstantiatableType(listType) ? (IList)null : (IList)CollectionUtils.CreateCollectionWrapper(Activator.CreateInstance(listType)))) : (!ReflectionUtils.IsInstantiatableType(listType) ? (listType != typeof(IList) ? (IList)null : (IList) new List <object>()) : (IList)Activator.CreateInstance(listType));
                }
            }
            if (list1 == null)
            {
                throw new InvalidOperationException(StringUtils.FormatWith("Cannot create and populate list type {0}.", (IFormatProvider)CultureInfo.InvariantCulture, (object)listType));
            }
            else
            {
                return(list1);
            }
        }
Exemplo n.º 2
0
        public static object CreateAndPopulateList(Type listType, Action <IList, bool> populateList)
        {
            ValidationUtils.ArgumentNotNull(listType, "listType");
            ValidationUtils.ArgumentNotNull(populateList, "populateList");
            bool  flag = false;
            IList list;
            Type  type;

            if (listType.IsArray)
            {
                list = new List <object>();
                flag = true;
            }
            else if (ReflectionUtils.InheritsGenericDefinition(listType, typeof(ReadOnlyCollection <>), out type))
            {
                Type type2 = type.GetGenericArguments()[0];
                Type type3 = ReflectionUtils.MakeGenericType(typeof(IEnumerable <>), new Type[]
                {
                    type2
                });
                bool flag2 = false;
                foreach (ConstructorInfo constructorInfo in listType.GetConstructors())
                {
                    IList <ParameterInfo> parameters = constructorInfo.GetParameters();
                    if (parameters.Count == 1 && type3.IsAssignableFrom(parameters[0].ParameterType))
                    {
                        flag2 = true;
                        break;
                    }
                }
                if (!flag2)
                {
                    throw new Exception("Read-only type {0} does not have a public constructor that takes a type that implements {1}.".FormatWith(CultureInfo.InvariantCulture, new object[]
                    {
                        listType,
                        type3
                    }));
                }
                list = CollectionUtils.CreateGenericList(type2);
                flag = 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 (ReflectionUtils.ImplementsGenericDefinition(listType, typeof(ICollection <>)))
            {
                if (ReflectionUtils.IsInstantiatableType(listType))
                {
                    list = CollectionUtils.CreateCollectionWrapper(Activator.CreateInstance(listType));
                }
                else
                {
                    list = null;
                }
            }
            else if (listType == typeof(BitArray))
            {
                list = new List <object>();
                flag = true;
            }
            else
            {
                list = null;
            }
            if (list == null)
            {
                throw new Exception("Cannot create and populate list type {0}.".FormatWith(CultureInfo.InvariantCulture, new object[]
                {
                    listType
                }));
            }
            populateList(list, flag);
            if (flag)
            {
                if (listType.IsArray)
                {
                    if (listType.GetArrayRank() > 1)
                    {
                        list = CollectionUtils.ToMultidimensionalArray(list, ReflectionUtils.GetCollectionItemType(listType), listType.GetArrayRank());
                    }
                    else
                    {
                        list = CollectionUtils.ToArray(((List <object>)list).ToArray(), ReflectionUtils.GetCollectionItemType(listType));
                    }
                }
                else if (ReflectionUtils.InheritsGenericDefinition(listType, typeof(ReadOnlyCollection <>)))
                {
                    list = (IList)ReflectionUtils.CreateInstance(listType, new object[]
                    {
                        list
                    });
                }
                else if (listType == typeof(BitArray))
                {
                    BitArray bitArray = new BitArray(list.Count);
                    for (int j = 0; j < list.Count; j++)
                    {
                        bitArray[j] = (bool)list[j];
                    }
                    return(bitArray);
                }
            }
            else if (list is IWrappedCollection)
            {
                return(((IWrappedCollection)list).UnderlyingCollection);
            }
            return(list);
        }
        public static object CreateAndPopulateList(Type listType, Action <IList, bool> populateList)
        {
            IList objs;
            Type  type;

            ValidationUtils.ArgumentNotNull(listType, "listType");
            ValidationUtils.ArgumentNotNull(populateList, "populateList");
            bool flag = false;

            if (listType.IsArray)
            {
                objs = new List <object>();
                flag = true;
            }
            else if (ReflectionUtils.InheritsGenericDefinition(listType, typeof(ReadOnlyCollection <>), out type))
            {
                Type genericArguments          = type.GetGenericArguments()[0];
                Type type1                     = ReflectionUtils.MakeGenericType(typeof(IEnumerable <>), new Type[] { genericArguments });
                bool flag1                     = false;
                ConstructorInfo[] constructors = listType.GetConstructors();
                int num = 0;
                while (num < (int)constructors.Length)
                {
                    IList <ParameterInfo> parameters = constructors[num].GetParameters();
                    if (parameters.Count != 1 || !type1.IsAssignableFrom(parameters[0].ParameterType))
                    {
                        num++;
                    }
                    else
                    {
                        flag1 = true;
                        break;
                    }
                }
                if (!flag1)
                {
                    throw new Exception("Read-only type {0} does not have a public constructor that takes a type that implements {1}.".FormatWith(CultureInfo.InvariantCulture, new object[] { listType, type1 }));
                }
                objs = CollectionUtils.CreateGenericList(genericArguments);
                flag = true;
            }
            else if (typeof(IList).IsAssignableFrom(listType))
            {
                if (ReflectionUtils.IsInstantiatableType(listType))
                {
                    objs = (IList)Activator.CreateInstance(listType);
                }
                else if (listType != typeof(IList))
                {
                    objs = null;
                }
                else
                {
                    objs = new List <object>();
                }
            }
            else if (ReflectionUtils.ImplementsGenericDefinition(listType, typeof(ICollection <>)))
            {
                if (!ReflectionUtils.IsInstantiatableType(listType))
                {
                    objs = null;
                }
                else
                {
                    objs = CollectionUtils.CreateCollectionWrapper(Activator.CreateInstance(listType));
                }
            }
            else if (listType != typeof(BitArray))
            {
                objs = null;
            }
            else
            {
                objs = new List <object>();
                flag = true;
            }
            if (objs == null)
            {
                throw new Exception("Cannot create and populate list type {0}.".FormatWith(CultureInfo.InvariantCulture, new object[] { listType }));
            }
            populateList(objs, flag);
            if (!flag)
            {
                if (objs is IWrappedCollection)
                {
                    return(((IWrappedCollection)objs).UnderlyingCollection);
                }
            }
            else if (listType.IsArray)
            {
                objs = (listType.GetArrayRank() <= 1 ? CollectionUtils.ToArray(((List <object>)objs).ToArray(), ReflectionUtils.GetCollectionItemType(listType)) : CollectionUtils.ToMultidimensionalArray(objs, ReflectionUtils.GetCollectionItemType(listType), listType.GetArrayRank()));
            }
            else if (ReflectionUtils.InheritsGenericDefinition(listType, typeof(ReadOnlyCollection <>)))
            {
                objs = (IList)ReflectionUtils.CreateInstance(listType, new object[] { objs });
            }
            else if (listType == typeof(BitArray))
            {
                BitArray bitArrays = new BitArray(objs.Count);
                for (int i = 0; i < objs.Count; i++)
                {
                    bitArrays[i] = (bool)objs[i];
                }
                return(bitArrays);
            }
            return(objs);
        }
Exemplo n.º 4
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);
        }