public static IWrappedDictionary CreateDictionaryWrapper(object dictionary)
        {
            ValidationUtils.ArgumentNotNull(dictionary, "dictionary");

            Type dictionaryDefinition;

            if (ReflectionUtils.ImplementsGenericDefinition(dictionary.GetType(), typeof(IDictionary <,>), out dictionaryDefinition))
            {
                Type dictionaryKeyType   = ReflectionUtils.GetDictionaryKeyType(dictionaryDefinition);
                Type dictionaryValueType = ReflectionUtils.GetDictionaryValueType(dictionaryDefinition);

                // Activator.CreateInstance throws AmbiguousMatchException. Manually invoke constructor
                Func <Type, IList <object>, object> instanceCreator = (t, a) =>
                {
                    ConstructorInfo c = t.GetConstructor(new[] { dictionaryDefinition });
                    return(c.Invoke(new[] { dictionary }));
                };

                return((IWrappedDictionary)ReflectionUtils.CreateGeneric(typeof(DictionaryWrapper <,>), new[] { dictionaryKeyType, dictionaryValueType }, instanceCreator, dictionary));
            }
            else if (dictionary is IDictionary)
            {
                return(new DictionaryWrapper <object, object>((IDictionary)dictionary));
            }
            else
            {
                throw new Exception("Can not create DictionaryWrapper for type {0}.".FormatWith(CultureInfo.InvariantCulture, dictionary.GetType()));
            }
        }
Esempio n. 2
0
        public static void GetDictionaryKeyValueTypes(Type dictionaryType, out Type keyType, out Type valueType)
        {
            ValidationUtils.ArgumentNotNull(dictionaryType, "type");
            Type type;

            if (ReflectionUtils.ImplementsGenericDefinition(dictionaryType, typeof(IDictionary <, >), out type))
            {
                if (type.IsGenericTypeDefinition)
                {
                    throw new Exception("Type {0} is not a dictionary.".FormatWith(CultureInfo.InvariantCulture, new object[]
                    {
                        dictionaryType
                    }));
                }
                Type[] genericArguments = type.GetGenericArguments();
                keyType   = genericArguments[0];
                valueType = genericArguments[1];
                return;
            }
            else
            {
                if (typeof(IDictionary).IsAssignableFrom(dictionaryType))
                {
                    keyType   = null;
                    valueType = null;
                    return;
                }
                throw new Exception("Type {0} is not a dictionary.".FormatWith(CultureInfo.InvariantCulture, new object[]
                {
                    dictionaryType
                }));
            }
        }
        public static IWrappedList CreateListWrapper(object list)
        {
            ValidationUtils.ArgumentNotNull(list, "list");

            Type listDefinition;

            if (ReflectionUtils.ImplementsGenericDefinition(list.GetType(), typeof(IList <>), out listDefinition))
            {
                Type collectionItemType = ReflectionUtils.GetCollectionItemType(listDefinition);

                // Activator.CreateInstance throws AmbiguousMatchException. Manually invoke constructor
                Func <Type, IList <object>, object> instanceCreator = (t, a) =>
                {
                    ConstructorInfo c = t.GetConstructor(new[] { listDefinition });
                    return(c.Invoke(new[] { list }));
                };

                return((IWrappedList)ReflectionUtils.CreateGeneric(typeof(ListWrapper <>), new[] { collectionItemType }, instanceCreator, list));
            }
            else if (list is IList)
            {
                return(new ListWrapper <object>((IList)list));
            }
            else
            {
                throw new Exception("Can not create ListWrapper for type {0}.".FormatWith(CultureInfo.InvariantCulture, list.GetType()));
            }
        }
Esempio n. 4
0
        public static IWrappedDictionary CreateDictionaryWrapper(object dictionary)
        {
            ValidationUtils.ArgumentNotNull(dictionary, "dictionary");
            Type dictionaryDefinition;

            if (ReflectionUtils.ImplementsGenericDefinition(dictionary.GetType(), typeof(IDictionary <,>), out dictionaryDefinition))
            {
                return((IWrappedDictionary)ReflectionUtils.CreateGeneric(typeof(DictionaryWrapper <,>), (IList <Type>) new Type[2]
                {
                    ReflectionUtils.GetDictionaryKeyType(dictionaryDefinition),
                    ReflectionUtils.GetDictionaryValueType(dictionaryDefinition)
                }, (Func <Type, IList <object>, object>)((t, a) => t.GetConstructor(new Type[1]
                {
                    dictionaryDefinition
                }).Invoke(new object[1]
                {
                    dictionary
                })), new object[1]
                {
                    dictionary
                }));
            }
            else if (dictionary is IDictionary)
            {
                return((IWrappedDictionary) new DictionaryWrapper <object, object>((IDictionary)dictionary));
            }
            else
            {
                throw new ArgumentException(StringUtils.FormatWith("Can not create DictionaryWrapper for type {0}.", (IFormatProvider)CultureInfo.InvariantCulture, (object)dictionary.GetType()), "dictionary");
            }
        }
Esempio n. 5
0
        public static Type GetCollectionItemType(Type type)
        {
            ValidationUtils.ArgumentNotNull(type, "type");
            if (type.IsArray)
            {
                return(type.GetElementType());
            }
            Type type2;

            if (ReflectionUtils.ImplementsGenericDefinition(type, typeof(IEnumerable <>), out type2))
            {
                if (type2.IsGenericTypeDefinition)
                {
                    throw new Exception("Type {0} is not a collection.".FormatWith(CultureInfo.InvariantCulture, new object[]
                    {
                        type
                    }));
                }
                return(type2.GetGenericArguments()[0]);
            }
            else
            {
                if (typeof(IEnumerable).IsAssignableFrom(type))
                {
                    return(null);
                }
                throw new Exception("Type {0} is not a collection.".FormatWith(CultureInfo.InvariantCulture, new object[]
                {
                    type
                }));
            }
        }
Esempio n. 6
0
        public static IWrappedCollection CreateCollectionWrapper(object list)
        {
            ValidationUtils.ArgumentNotNull(list, "list");
            Type collectionDefinition;

            if (ReflectionUtils.ImplementsGenericDefinition(list.GetType(), typeof(ICollection <>), out collectionDefinition))
            {
                return((IWrappedCollection)ReflectionUtils.CreateGeneric(typeof(CollectionWrapper <>), (IList <Type>) new Type[1]
                {
                    ReflectionUtils.GetCollectionItemType(collectionDefinition)
                }, (Func <Type, IList <object>, object>)((t, a) => t.GetConstructor(new Type[1]
                {
                    collectionDefinition
                }).Invoke(new object[1]
                {
                    list
                })), new object[1]
                {
                    list
                }));
            }
            else if (list is IList)
            {
                return((IWrappedCollection) new CollectionWrapper <object>((IList)list));
            }
            else
            {
                throw new ArgumentException(StringUtils.FormatWith("Can not create ListWrapper for type {0}.", (IFormatProvider)CultureInfo.InvariantCulture, (object)list.GetType()), "list");
            }
        }
Esempio n. 7
0
        public static void GetDictionaryKeyValueTypes(
            Type dictionaryType,
            out Type keyType,
            out Type valueType)
        {
            ValidationUtils.ArgumentNotNull((object)dictionaryType, nameof(dictionaryType));
            Type implementingType;

            if (ReflectionUtils.ImplementsGenericDefinition(dictionaryType, typeof(IDictionary <,>), out implementingType))
            {
                if (implementingType.IsGenericTypeDefinition())
                {
                    throw new Exception("Type {0} is not a dictionary.".FormatWith((IFormatProvider)CultureInfo.InvariantCulture, (object)dictionaryType));
                }
                Type[] genericArguments = implementingType.GetGenericArguments();
                keyType   = genericArguments[0];
                valueType = genericArguments[1];
            }
            else
            {
                if (!typeof(IDictionary).IsAssignableFrom(dictionaryType))
                {
                    throw new Exception("Type {0} is not a dictionary.".FormatWith((IFormatProvider)CultureInfo.InvariantCulture, (object)dictionaryType));
                }
                keyType   = (Type)null;
                valueType = (Type)null;
            }
        }
Esempio n. 8
0
 public static IWrappedDictionary CreateDictionaryWrapper(object dictionary)
 {
     ValidationUtils.ArgumentNotNull(dictionary, "dictionary");
     if (ReflectionUtils.ImplementsGenericDefinition(dictionary.GetType(), typeof(IDictionary <, >), out var dictionaryDefinition))
     {
         Type dictionaryKeyType   = ReflectionUtils.GetDictionaryKeyType(dictionaryDefinition);
         Type dictionaryValueType = ReflectionUtils.GetDictionaryValueType(dictionaryDefinition);
         Func <Type, IList <object>, object> instanceCreator = delegate(Type t, IList <object> a)
         {
             ConstructorInfo constructor = t.GetConstructor(new Type[1] {
                 dictionaryDefinition
             });
             return(constructor.Invoke(new object[1] {
                 dictionary
             }));
         };
         return((IWrappedDictionary)ReflectionUtils.CreateGeneric(typeof(DictionaryWrapper <, >), new Type[2] {
             dictionaryKeyType, dictionaryValueType
         }, instanceCreator, dictionary));
     }
     if (dictionary is IDictionary)
     {
         return(new DictionaryWrapper <object, object>((IDictionary)dictionary));
     }
     throw new Exception("Can not create DictionaryWrapper for type {0}.".FormatWith(CultureInfo.InvariantCulture, dictionary.GetType()));
 }
Esempio n. 9
0
 public static IWrappedList CreateListWrapper(object list)
 {
     ValidationUtils.ArgumentNotNull(list, "list");
     if (ReflectionUtils.ImplementsGenericDefinition(list.GetType(), typeof(IList <>), out var listDefinition))
     {
         Type collectionItemType = ReflectionUtils.GetCollectionItemType(listDefinition);
         Func <Type, IList <object>, object> instanceCreator = delegate(Type t, IList <object> a)
         {
             ConstructorInfo constructor = t.GetConstructor(new Type[1] {
                 listDefinition
             });
             return(constructor.Invoke(new object[1] {
                 list
             }));
         };
         return((IWrappedList)ReflectionUtils.CreateGeneric(typeof(ListWrapper <>), new Type[1] {
             collectionItemType
         }, instanceCreator, list));
     }
     if (list is IList)
     {
         return(new ListWrapper <object>((IList)list));
     }
     throw new Exception("Can not create ListWrapper for type {0}.".FormatWith(CultureInfo.InvariantCulture, list.GetType()));
 }
Esempio n. 10
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);
            }
        }
 public static bool IsDictionaryType(Type type)
 {
     ValidationUtils.ArgumentNotNull(type, "type");
     if (typeof(IDictionary).IsAssignableFrom(type))
     {
         return(true);
     }
     if (ReflectionUtils.ImplementsGenericDefinition(type, typeof(IDictionary <,>)))
     {
         return(true);
     }
     return(false);
 }
 public static bool IsCollectionType(Type type)
 {
     ValidationUtils.ArgumentNotNull(type, "type");
     if (type.IsArray)
     {
         return(true);
     }
     if (typeof(ICollection).IsAssignableFrom(type))
     {
         return(true);
     }
     if (ReflectionUtils.ImplementsGenericDefinition(type, typeof(ICollection <>)))
     {
         return(true);
     }
     return(false);
 }
Esempio n. 13
0
        public static bool IsDictionaryType(Type type)
        {
            ValidationUtils.ArgumentNotNull(type, "type");

            if (typeof(IDictionary).IsAssignableFrom(type))
            {
                return(true);
            }
            if (ReflectionUtils.ImplementsGenericDefinition(type, typeof(IDictionary <,>)))
            {
                return(true);
            }
#if !(NET40 || NET35 || NET20 || PORTABLE40)
            if (ReflectionUtils.ImplementsGenericDefinition(type, typeof(IReadOnlyDictionary <,>)))
            {
                return(true);
            }
#endif

            return(false);
        }
        public static bool IsDictionaryType(Type type)
        {
            ValidationUtils.ArgumentNotNull(type, nameof(type));

            if (typeof(IDictionary).IsAssignableFrom(type))
            {
                return(true);
            }
            if (ReflectionUtils.ImplementsGenericDefinition(type, typeof(IDictionary <,>)))
            {
                return(true);
            }
#if HAVE_READ_ONLY_COLLECTIONS
            if (ReflectionUtils.ImplementsGenericDefinition(type, typeof(IReadOnlyDictionary <,>)))
            {
                return(true);
            }
#endif

            return(false);
        }
        public static IWrappedList CreateListWrapper(object list)
        {
            Type type;

            ValidationUtils.ArgumentNotNull(list, "list");
            if (!ReflectionUtils.ImplementsGenericDefinition(list.GetType(), typeof(IList <>), out type))
            {
                if (!(list is IList))
                {
                    throw new Exception("Can not create ListWrapper for type {0}.".FormatWith(CultureInfo.InvariantCulture, new object[] { list.GetType() }));
                }
                return(new ListWrapper <object>((IList)list));
            }
            Type collectionItemType = ReflectionUtils.GetCollectionItemType(type);
            Func <Type, IList <object>, object> func = (Type t, IList <object> a) => t.GetConstructor(new Type[] { type }).Invoke(new object[] { list });
            Type type1 = typeof(ListWrapper <>);

            Type[]   typeArray = new Type[] { collectionItemType };
            object[] objArray  = new object[] { list };
            return((IWrappedList)ReflectionUtils.CreateGeneric(type1, (IList <Type>)typeArray, func, objArray));
        }
        public static IWrappedDictionary CreateDictionaryWrapper(object dictionary)
        {
            Type type;

            ValidationUtils.ArgumentNotNull(dictionary, "dictionary");
            if (!ReflectionUtils.ImplementsGenericDefinition(dictionary.GetType(), typeof(IDictionary <,>), out type))
            {
                if (!(dictionary is IDictionary))
                {
                    throw new Exception("Can not create DictionaryWrapper for type {0}.".FormatWith(CultureInfo.InvariantCulture, new object[] { dictionary.GetType() }));
                }
                return(new DictionaryWrapper <object, object>((IDictionary)dictionary));
            }
            Type dictionaryKeyType   = ReflectionUtils.GetDictionaryKeyType(type);
            Type dictionaryValueType = ReflectionUtils.GetDictionaryValueType(type);
            Func <Type, IList <object>, object> func = (Type t, IList <object> a) => t.GetConstructor(new Type[] { type }).Invoke(new object[] { dictionary });
            Type type1 = typeof(DictionaryWrapper <,>);

            Type[]   typeArray = new Type[] { dictionaryKeyType, dictionaryValueType };
            object[] objArray  = new object[] { dictionary };
            return((IWrappedDictionary)ReflectionUtils.CreateGeneric(type1, (IList <Type>)typeArray, func, objArray));
        }
Esempio n. 17
0
        /// <summary>Gets the type of the typed collection's items.</summary>
        /// <param name="type">The type.</param>
        /// <returns>The type of the typed collection's items.</returns>
        public static Type GetCollectionItemType(Type type)
        {
            ValidationUtils.ArgumentNotNull((object)type, nameof(type));
            if (type.IsArray)
            {
                return(type.GetElementType());
            }
            Type implementingType;

            if (ReflectionUtils.ImplementsGenericDefinition(type, typeof(IEnumerable <>), out implementingType))
            {
                if (implementingType.IsGenericTypeDefinition())
                {
                    throw new Exception("Type {0} is not a collection.".FormatWith((IFormatProvider)CultureInfo.InvariantCulture, (object)type));
                }
                return(implementingType.GetGenericArguments()[0]);
            }
            if (typeof(IEnumerable).IsAssignableFrom(type))
            {
                return((Type)null);
            }
            throw new Exception("Type {0} is not a collection.".FormatWith((IFormatProvider)CultureInfo.InvariantCulture, (object)type));
        }
Esempio n. 18
0
 public static bool IsDictionaryType(Type type)
 {
     ValidationUtils.ArgumentNotNull(type, "type");
     return(typeof(IDictionary).IsAssignableFrom(type) || (ReflectionUtils.ImplementsGenericDefinition(type, typeof(IDictionary <,>)) || ReflectionUtils.ImplementsGenericDefinition(type, typeof(IReadOnlyDictionary <,>))));
 }
Esempio n. 19
0
        public static object 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 = 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 (ReflectionUtils.ImplementsGenericDefinition(listType, typeof(ICollection <>)))
            {
                if (ReflectionUtils.IsInstantiatableType(listType))
                {
                    list = CreateCollectionWrapper(Activator.CreateInstance(listType));
                }
                else
                {
                    list = null;
                }
            }
            else if (listType == typeof(BitArray))
            {
                // 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
            {
                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)
                {
                    if (listType.GetArrayRank() > 1)
                    {
                        list = ToMultidimensionalArray(list, ReflectionUtils.GetCollectionItemType(listType), listType.GetArrayRank());
                    }
                    else
                    {
                        list = ToArray(((List <object>)list).ToArray(), ReflectionUtils.GetCollectionItemType(listType));
                    }
                }
                else if (ReflectionUtils.InheritsGenericDefinition(listType, typeof(ReadOnlyCollection <>)))
                {
                    list = (IList)ReflectionUtils.CreateInstance(listType, list);
                }
                else if (listType == typeof(BitArray))
                {
                    var newBitArray = new BitArray(list.Count);

                    for (var i = 0; i < list.Count; i++)
                    {
                        newBitArray[i] = (bool)list[i];
                    }

                    return(newBitArray);
                }
            }
            else if (list is IWrappedCollection)
            {
                return(((IWrappedCollection)list).UnderlyingCollection);
            }

            return(list);
        }
Esempio n. 20
0
        // Token: 0x06000E29 RID: 3625 RVA: 0x00051B74 File Offset: 0x0004FD74
        public static bool ImplementsGenericDefinition(Type type, Type genericInterfaceDefinition)
        {
            Type type2;

            return(ReflectionUtils.ImplementsGenericDefinition(type, genericInterfaceDefinition, out type2));
        }
Esempio n. 21
0
        public static IList CreateList(Type listType, out bool isReadOnlyOrFixedSize)
        {
            ValidationUtils.ArgumentNotNull(listType, "listType");

            IList list;
            Type  collectionType;

            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 = 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 (ReflectionUtils.ImplementsGenericDefinition(listType, typeof(ICollection <>)))
            {
                if (ReflectionUtils.IsInstantiatableType(listType))
                {
                    list = CreateCollectionWrapper(Activator.CreateInstance(listType));
                }
                else
                {
                    list = null;
                }
            }
            else
            {
                list = null;
            }

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

            return(list);
        }
Esempio n. 22
0
 public static bool IsDictionaryType(Type type)
 {
     ValidationUtils.ArgumentNotNull((object)type, nameof(type));
     return(typeof(IDictionary).IsAssignableFrom(type) || ReflectionUtils.ImplementsGenericDefinition(type, typeof(IDictionary <,>)));
 }
Esempio n. 23
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);
        }
Esempio n. 24
0
 public static bool IsCollectionType(Type type)
 {
     ValidationUtils.ArgumentNotNull(type, "type");
     return(type.IsArray || typeof(ICollection).IsAssignableFrom(type) || ReflectionUtils.ImplementsGenericDefinition(type, typeof(ICollection <>)));
 }
 public static bool IsListType(Type type)
 {
     ValidationUtils.ArgumentNotNull(type, "type");
     return(type.get_IsArray() || typeof(IList).IsAssignableFrom(type) || ReflectionUtils.ImplementsGenericDefinition(type, typeof(IList)));
 }
        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);
        }
        public static object CreateAndPopulateList(Type listType, Action <IList, bool> populateList)
        {
            ValidationUtils.ArgumentNotNull(listType, "listType");
            ValidationUtils.ArgumentNotNull(populateList, "populateList");
            bool  flag = false;
            IList list;
            Type  implementingType;

            if (listType.IsArray)
            {
                list = new List <object>();
                flag = true;
            }
            else if (!ReflectionUtils.InheritsGenericDefinition(listType, typeof(ReadOnlyCollection <>), out implementingType))
            {
                list = (typeof(IList).IsAssignableFrom(listType) ? (ReflectionUtils.IsInstantiatableType(listType) ? ((IList)Activator.CreateInstance(listType)) : ((listType != typeof(IList)) ? null : new List <object>())) : ((!ReflectionUtils.ImplementsGenericDefinition(listType, typeof(ICollection <>))) ? null : ((!ReflectionUtils.IsInstantiatableType(listType)) ? null : CreateCollectionWrapper(Activator.CreateInstance(listType)))));
            }
            else
            {
                Type type  = implementingType.GetGenericArguments()[0];
                Type type2 = ReflectionUtils.MakeGenericType(typeof(IEnumerable <>), type);
                bool flag2 = false;
                ConstructorInfo[] constructors = listType.GetConstructors();
                foreach (ConstructorInfo constructorInfo in constructors)
                {
                    IList <ParameterInfo> parameters = constructorInfo.GetParameters();
                    if (parameters.Count == 1 && type2.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, listType, type2));
                }
                list = CreateGenericList(type);
                flag = true;
            }
            if (list == null)
            {
                throw new Exception("Cannot create and populate list type {0}.".FormatWith(CultureInfo.InvariantCulture, listType));
            }
            populateList(list, flag);
            if (flag)
            {
                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);
                }
            }
            else if (list is IWrappedCollection)
            {
                return(((IWrappedCollection)list).UnderlyingCollection);
            }
            return(list);
        }