public static Type MakeGenericType(Type genericTypeDefinition, params Type[] innerTypes) { ValidationUtils.ArgumentNotNull(genericTypeDefinition, "genericTypeDefinition"); ValidationUtils.ArgumentNotNullOrEmpty(innerTypes, "innerTypes"); ValidationUtils.ArgumentConditionTrue(TypeExtensions.IsGenericTypeDefinition(genericTypeDefinition), "genericTypeDefinition", StringUtils.FormatWith("Type {0} is not a generic type definition.", CultureInfo.InvariantCulture, genericTypeDefinition)); return(genericTypeDefinition.MakeGenericType(innerTypes)); }
public static void GetDictionaryKeyValueTypes(Type dictionaryType, out Type keyType, out Type valueType) { ValidationUtils.ArgumentNotNull(dictionaryType, "type"); Type implementingType; if (ImplementsGenericDefinition(dictionaryType, typeof(IDictionary <,>), out implementingType)) { if (TypeExtensions.IsGenericTypeDefinition(implementingType)) { throw new Exception(StringUtils.FormatWith("Type {0} is not a dictionary.", CultureInfo.InvariantCulture, dictionaryType)); } Type[] genericArguments = implementingType.GetGenericArguments(); keyType = genericArguments[0]; valueType = genericArguments[1]; } else { if (!typeof(IDictionary).IsAssignableFrom(dictionaryType)) { throw new Exception(StringUtils.FormatWith("Type {0} is not a dictionary.", CultureInfo.InvariantCulture, dictionaryType)); } keyType = null; valueType = null; } }
public static bool ImplementsGenericDefinition(Type type, Type genericInterfaceDefinition, out Type implementingType) { ValidationUtils.ArgumentNotNull(type, "type"); ValidationUtils.ArgumentNotNull(genericInterfaceDefinition, "genericInterfaceDefinition"); if (!TypeExtensions.IsInterface(genericInterfaceDefinition) || !TypeExtensions.IsGenericTypeDefinition(genericInterfaceDefinition)) { throw new ArgumentNullException(StringUtils.FormatWith("'{0}' is not a generic interface definition.", CultureInfo.InvariantCulture, genericInterfaceDefinition)); } if (TypeExtensions.IsInterface(type) && TypeExtensions.IsGenericType(type)) { Type genericTypeDefinition = type.GetGenericTypeDefinition(); if (genericInterfaceDefinition == genericTypeDefinition) { implementingType = type; return(true); } } foreach (Type type1 in type.GetInterfaces()) { if (TypeExtensions.IsGenericType(type1)) { Type genericTypeDefinition = type1.GetGenericTypeDefinition(); if (genericInterfaceDefinition == genericTypeDefinition) { implementingType = type1; return(true); } } } implementingType = null; return(false); }
public static bool InheritsGenericDefinition(Type type, Type genericClassDefinition, out Type implementingType) { ValidationUtils.ArgumentNotNull(type, "type"); ValidationUtils.ArgumentNotNull(genericClassDefinition, "genericClassDefinition"); if (!TypeExtensions.IsClass(genericClassDefinition) || !TypeExtensions.IsGenericTypeDefinition(genericClassDefinition)) { throw new ArgumentNullException(StringUtils.FormatWith("'{0}' is not a generic class definition.", CultureInfo.InvariantCulture, genericClassDefinition)); } return(InheritsGenericDefinitionInternal(type, genericClassDefinition, out implementingType)); }
public static Type GetCollectionItemType(Type type) { ValidationUtils.ArgumentNotNull(type, "type"); if (type.IsArray) { return(type.GetElementType()); } Type implementingType; if (ImplementsGenericDefinition(type, typeof(IEnumerable <>), out implementingType)) { if (TypeExtensions.IsGenericTypeDefinition(implementingType)) { throw new Exception(StringUtils.FormatWith("Type {0} is not a collection.", CultureInfo.InvariantCulture, type)); } return(implementingType.GetGenericArguments()[0]); } if (typeof(IEnumerable).IsAssignableFrom(type)) { return(null); } throw new Exception(StringUtils.FormatWith("Type {0} is not a collection.", CultureInfo.InvariantCulture, type)); }
public static bool IsInstantiatableType(Type t) { ValidationUtils.ArgumentNotNull(t, "t"); return(!TypeExtensions.IsAbstract(t) && !TypeExtensions.IsInterface(t) && (!t.IsArray && !TypeExtensions.IsGenericTypeDefinition(t)) && (!(t == typeof(void)) && HasDefaultConstructor(t))); }