예제 #1
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);
            }

            return(false);
        }
예제 #2
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);
        }
예제 #3
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);
        }