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())); } }
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); }