Пример #1
0
        /// <summary>
        /// Determines whether the property is an indexed property.
        /// </summary>
        /// <param name="property">The property.</param>
        /// <returns>
        ///     <c>true</c> if the property is an indexed property; otherwise, <c>false</c>.
        /// </returns>
        public static bool IsIndexedProperty(PropertyInfo property)
        {
            ValidationUtils.ArgumentNotNull(property, "property");

            return(property.GetIndexParameters().Length > 0);
        }
Пример #2
0
        public static bool IsNullableType(Type t)
        {
            ValidationUtils.ArgumentNotNull(t, "t");

            return(t.IsGenericType() && t.GetGenericTypeDefinition() == typeof(Nullable <>));
        }
Пример #3
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
            {
                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);
                }
            }
            else if (list is IWrappedCollection)
            {
                return(((IWrappedCollection)list).UnderlyingCollection);
            }

            return(list);
        }
Пример #4
0
        public static IEnumerable <T> CastValid <T>(this IEnumerable enumerable)
        {
            ValidationUtils.ArgumentNotNull(enumerable, "enumerable");

            return(enumerable.Cast <object>().Where(o => o is T).Cast <T>());
        }
Пример #5
0
        public static IList CreateGenericList(Type listType)
        {
            ValidationUtils.ArgumentNotNull(listType, "listType");

            return((IList)ReflectionUtils.CreateGeneric(typeof(List <>), listType));
        }
 public CollectionWrapper(ICollection <T> list)
 {
     ValidationUtils.ArgumentNotNull(list, "list");
     this._genericCollection = list;
 }