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 IWrappedDictionary CreateDictionaryWrapper(object dictionary) { ValidationUtils.ArgumentNotNull(dictionary, "dictionary"); Type dictionaryDefinition; if (ReflectionUtils.ImplementsGenericDefinition(dictionary.GetType(), typeof(IDictionary <,>), out dictionaryDefinition)) { Type dictionaryKeyType = ReflectionUtils.GetDictionaryKeyType(dictionaryDefinition); Type dictionaryValueType = ReflectionUtils.GetDictionaryValueType(dictionaryDefinition); // Activator.CreateInstance throws AmbiguousMatchException. Manually invoke constructor Func <Type, IList <object>, object> instanceCreator = (t, a) => { ConstructorInfo c = t.GetConstructor(new[] { dictionaryDefinition }); return(c.Invoke(new[] { dictionary })); }; return((IWrappedDictionary)ReflectionUtils.CreateGeneric(typeof(DictionaryWrapper <,>), new[] { dictionaryKeyType, dictionaryValueType }, instanceCreator, dictionary)); } else if (dictionary is IDictionary) { return(new DictionaryWrapper <object, object>((IDictionary)dictionary)); } else { throw new Exception("Can not create DictionaryWrapper for type {0}.".FormatWith(CultureInfo.InvariantCulture, dictionary.GetType())); } }
public static object CreateGeneric(Type genericTypeDefinition, Type innerType, params object[] args) { return(ReflectionUtils.CreateGeneric(genericTypeDefinition, new Type[] { innerType }, args)); }
public static IDictionary CreateGenericDictionary(Type keyType, Type valueType) { ValidationUtils.ArgumentNotNull(keyType, "keyType"); ValidationUtils.ArgumentNotNull(valueType, "valueType"); return((IDictionary)ReflectionUtils.CreateGeneric(typeof(Dictionary <,>), keyType, valueType)); }
public static IWrappedDictionary CreateDictionaryWrapper(object dictionary) { ValidationUtils.ArgumentNotNull(dictionary, "dictionary"); if (ReflectionUtils.ImplementsGenericDefinition(dictionary.GetType(), typeof(IDictionary <, >), out Type dictionaryDefinition)) { Type dictionaryKeyType = ReflectionUtils.GetDictionaryKeyType(dictionaryDefinition); Type dictionaryValueType = ReflectionUtils.GetDictionaryValueType(dictionaryDefinition); Func <Type, IList <object>, object> instanceCreator = delegate(Type t, IList <object> a) { ConstructorInfo constructor = t.GetConstructor(new Type[1] { dictionaryDefinition }); return(constructor.Invoke(new object[1] { dictionary })); }; return((IWrappedDictionary)ReflectionUtils.CreateGeneric(typeof(DictionaryWrapper <, >), new Type[2] { dictionaryKeyType, dictionaryValueType }, instanceCreator, dictionary)); } if (dictionary is IDictionary) { return(new DictionaryWrapper <object, object>((IDictionary)dictionary)); } throw new Exception("Can not create DictionaryWrapper for type {0}.".FormatWith(CultureInfo.InvariantCulture, dictionary.GetType())); }
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 IWrappedDictionary CreateDictionaryWrapper(object dictionary) { ValidationUtils.ArgumentNotNull(dictionary, "dictionary"); Type dictionaryDefinition; if (ReflectionUtils.ImplementsGenericDefinition(dictionary.GetType(), typeof(IDictionary <,>), out dictionaryDefinition)) { return((IWrappedDictionary)ReflectionUtils.CreateGeneric(typeof(DictionaryWrapper <,>), (IList <Type>) new Type[2] { ReflectionUtils.GetDictionaryKeyType(dictionaryDefinition), ReflectionUtils.GetDictionaryValueType(dictionaryDefinition) }, (Func <Type, IList <object>, object>)((t, a) => t.GetConstructor(new Type[1] { dictionaryDefinition }).Invoke(new object[1] { dictionary })), new object[1] { dictionary })); } else if (dictionary is IDictionary) { return((IWrappedDictionary) new DictionaryWrapper <object, object>((IDictionary)dictionary)); } else { throw new ArgumentException(StringUtils.FormatWith("Can not create DictionaryWrapper for type {0}.", (IFormatProvider)CultureInfo.InvariantCulture, (object)dictionary.GetType()), "dictionary"); } }
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 IWrappedDictionary CreateDictionaryWrapper(object dictionary) { Type type; ValidationUtils.ArgumentNotNull(dictionary, "dictionary"); if (!ReflectionUtils.ImplementsGenericDefinition(dictionary.GetType(), typeof(IDictionary <,>), out type)) { if (!(dictionary is IDictionary)) { throw new Exception("Can not create DictionaryWrapper for type {0}.".FormatWith(CultureInfo.InvariantCulture, new object[] { dictionary.GetType() })); } return(new DictionaryWrapper <object, object>((IDictionary)dictionary)); } Type dictionaryKeyType = ReflectionUtils.GetDictionaryKeyType(type); Type dictionaryValueType = ReflectionUtils.GetDictionaryValueType(type); Func <Type, IList <object>, object> func = (Type t, IList <object> a) => t.GetConstructor(new Type[] { type }).Invoke(new object[] { dictionary }); Type type1 = typeof(DictionaryWrapper <,>); Type[] typeArray = new Type[] { dictionaryKeyType, dictionaryValueType }; object[] objArray = new object[] { dictionary }; return((IWrappedDictionary)ReflectionUtils.CreateGeneric(type1, (IList <Type>)typeArray, func, objArray)); }
public static IList CreateGenericList(Type listType) { ValidationUtils.ArgumentNotNull(listType, "listType"); return((IList)ReflectionUtils.CreateGeneric(typeof(List <>), listType)); }
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 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)); }