public static object CreateGeneric(Type genericTypeDefinition, IList <Type> innerTypes, Func <Type, IList <object>, object> instanceCreator, params object[] args) { ValidationUtils.ArgumentNotNull(genericTypeDefinition, "genericTypeDefinition"); ValidationUtils.ArgumentNotNullOrEmpty <Type>(innerTypes, "innerTypes"); ValidationUtils.ArgumentNotNull(instanceCreator, "createInstance"); Type type = ReflectionUtils.MakeGenericType(genericTypeDefinition, innerTypes.ToArray <Type>()); return(instanceCreator(type, args)); }
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 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 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); }
public static IList CreateAndPopulateList(Type listType, Action <IList> populateList) { ValidationUtils.ArgumentNotNull(listType, "listType"); ValidationUtils.ArgumentNotNull(populateList, "populateList"); IList list; Type readOnlyCollectionType; 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 ArrayList(); isReadOnlyOrFixedSize = true; } else if (ReflectionUtils.IsSubClass(listType, typeof(ReadOnlyCollection <>), out readOnlyCollectionType)) { Type readOnlyCollectionContentsType = readOnlyCollectionType.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(string.Format("Readonly type {0} does not have a public constructor that takes a type that implements {1}.", 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) && ReflectionUtils.IsInstantiatableType(listType)) { list = (IList)Activator.CreateInstance(listType); } else { throw new Exception(string.Format("Cannot create and populate list type {0}.", listType)); } populateList(list); // create readonly and fixed sized collections using the temporary list if (isReadOnlyOrFixedSize) { if (listType.IsArray) { list = ((ArrayList)list).ToArray(ReflectionUtils.GetListItemType(listType)); } else if (ReflectionUtils.IsSubClass(listType, typeof(ReadOnlyCollection <>))) { list = (IList)Activator.CreateInstance(listType, list); } } 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); }