public override Func <T> CreateDefaultConstructor <T>(Type type) { if (type.IsValueType) { return(() => (T)ReflectionUtils.CreateInstance(type)); } ConstructorInfo constructorInfo = ReflectionUtils.GetDefaultConstructor(type, true); return(() => (T)constructorInfo.Invoke(null)); }
public override Func <T> CreateDefaultConstructor <T>(Type type) { ValidationUtils.ArgumentNotNull(type, "type"); if (type.IsValueType) { return(() => (T)((object)ReflectionUtils.CreateInstance(type, new object[0]))); } ConstructorInfo constructorInfo = ReflectionUtils.GetDefaultConstructor(type, true); return(() => (T)((object)constructorInfo.Invoke(null))); }
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 CreateGeneric(Type genericTypeDefinition, IList <Type> innerTypes, params object[] args) { return(ReflectionUtils.CreateGeneric(genericTypeDefinition, innerTypes, (Type t, IList <object> a) => ReflectionUtils.CreateInstance(t, a.ToArray <object>()), args)); }
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); }
public static object CreateGeneric(Type genericTypeDefinition, IList <Type> innerTypes, params object[] args) { return(ReflectionUtils.CreateGeneric(genericTypeDefinition, innerTypes, (Func <Type, IList <object>, object>)((t, a) => ReflectionUtils.CreateInstance(t, Enumerable.ToArray <object>((IEnumerable <object>)a))), args)); }