public static IWrappedCollection CreateCollectionWrapper(object list) { ValidationUtils.ArgumentNotNull(list, "list"); Type collectionDefinition; if (ReflectionUtils.ImplementsGenericDefinition(list.GetType(), typeof(ICollection <>), out collectionDefinition)) { Type collectionItemType = ReflectionUtils.GetCollectionItemType(collectionDefinition); // Activator.CreateInstance throws AmbiguousMatchException. Manually invoke constructor Func <Type, IList <object>, object> instanceCreator = (t, a) => { ConstructorInfo c = t.GetConstructor(new[] { collectionDefinition }); return(c.Invoke(new[] { list })); }; return((IWrappedCollection)ReflectionUtils.CreateGeneric(typeof(CollectionWrapper <>), new[] { collectionItemType }, instanceCreator, list)); } else if (list is IList) { return(new CollectionWrapper <object>((IList)list)); } else { throw new ArgumentException("Can not create ListWrapper for type {0}.".FormatWith(CultureInfo.InvariantCulture, list.GetType()), "list"); } }
public static bool ItemsUnitializedValue <T>(IList <T> list) { ValidationUtils.ArgumentNotNull(list, "list"); Type collectionItemType = ReflectionUtils.GetCollectionItemType(list.GetType()); if (!collectionItemType.IsValueType) { if (!collectionItemType.IsClass) { throw new Exception("Type {0} is neither a ValueType or a Class.".FormatWith(CultureInfo.InvariantCulture, new object[] { collectionItemType })); } for (int i = 0; i < list.Count; i++) { if (list[i] != null) { return(false); } } } else { object obj = ReflectionUtils.CreateUnitializedValue(collectionItemType); for (int j = 0; j < list.Count; j++) { if (!list[j].Equals(obj)) { return(false); } } } return(true); }
public static IWrappedList CreateListWrapper(object list) { ValidationUtils.ArgumentNotNull(list, "list"); if (ReflectionUtils.ImplementsGenericDefinition(list.GetType(), typeof(IList <>), out Type 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())); }
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"); } }
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 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); }
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); }
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); }