public static Type MakeGenericType(Type genericTypeDefinition, params Type[] innerTypes) { ValidationUtils.ArgumentNotNull((object)genericTypeDefinition, "genericTypeDefinition"); ValidationUtils.ArgumentNotNullOrEmpty <Type>((ICollection <Type>)innerTypes, "innerTypes"); ValidationUtils.ArgumentConditionTrue(TypeExtensions.IsGenericTypeDefinition(genericTypeDefinition), "genericTypeDefinition", StringUtils.FormatWith("Type {0} is not a generic type definition.", (IFormatProvider)CultureInfo.InvariantCulture, (object)genericTypeDefinition)); return(genericTypeDefinition.MakeGenericType(innerTypes)); }
public static void GetDictionaryKeyValueTypes(Type dictionaryType, out Type keyType, out Type valueType) { ValidationUtils.ArgumentNotNull((object)dictionaryType, "type"); Type implementingType; if (ReflectionUtils.ImplementsGenericDefinition(dictionaryType, typeof(IDictionary <,>), out implementingType)) { if (TypeExtensions.IsGenericTypeDefinition(implementingType)) { throw new Exception(StringUtils.FormatWith("Type {0} is not a dictionary.", (IFormatProvider)CultureInfo.InvariantCulture, (object)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.", (IFormatProvider)CultureInfo.InvariantCulture, (object)dictionaryType)); } keyType = (Type)null; valueType = (Type)null; } }
public static Type GetCollectionItemType(Type type) { ValidationUtils.ArgumentNotNull((object)type, "type"); if (type.IsArray) { return(type.GetElementType()); } Type implementingType; if (ReflectionUtils.ImplementsGenericDefinition(type, typeof(IEnumerable <>), out implementingType)) { if (TypeExtensions.IsGenericTypeDefinition(implementingType)) { throw new Exception(StringUtils.FormatWith("Type {0} is not a collection.", (IFormatProvider)CultureInfo.InvariantCulture, (object)type)); } else { return(implementingType.GetGenericArguments()[0]); } } else if (typeof(IEnumerable).IsAssignableFrom(type)) { return((Type)null); } else { throw new Exception(StringUtils.FormatWith("Type {0} is not a collection.", (IFormatProvider)CultureInfo.InvariantCulture, (object)type)); } }
public static bool ImplementsGenericDefinition(Type type, Type genericInterfaceDefinition, out Type implementingType) { ValidationUtils.ArgumentNotNull((object)type, "type"); ValidationUtils.ArgumentNotNull((object)genericInterfaceDefinition, "genericInterfaceDefinition"); if (!TypeExtensions.IsInterface(genericInterfaceDefinition) || !TypeExtensions.IsGenericTypeDefinition(genericInterfaceDefinition)) { throw new ArgumentNullException(StringUtils.FormatWith("'{0}' is not a generic interface definition.", (IFormatProvider)CultureInfo.InvariantCulture, (object)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 = (Type)null; return(false); }
public static bool InheritsGenericDefinition(Type type, Type genericClassDefinition, out Type implementingType) { ValidationUtils.ArgumentNotNull((object)type, "type"); ValidationUtils.ArgumentNotNull((object)genericClassDefinition, "genericClassDefinition"); if (!TypeExtensions.IsClass(genericClassDefinition) || !TypeExtensions.IsGenericTypeDefinition(genericClassDefinition)) { throw new ArgumentNullException(StringUtils.FormatWith("'{0}' is not a generic class definition.", (IFormatProvider)CultureInfo.InvariantCulture, (object)genericClassDefinition)); } else { return(ReflectionUtils.InheritsGenericDefinitionInternal(type, genericClassDefinition, out implementingType)); } }
public static object Convert(object initialValue, CultureInfo culture, Type targetType) { if (initialValue == null) { throw new ArgumentNullException("initialValue"); } if (ReflectionUtils.IsNullableType(targetType)) { targetType = Nullable.GetUnderlyingType(targetType); } Type type = initialValue.GetType(); if (targetType == type) { return(initialValue); } if (ConvertUtils.IsConvertible(initialValue) && ConvertUtils.IsConvertible(targetType)) { if (TypeExtensions.IsEnum(targetType)) { if (initialValue is string) { return(Enum.Parse(targetType, initialValue.ToString(), true)); } if (ConvertUtils.IsInteger(initialValue)) { return(Enum.ToObject(targetType, initialValue)); } } return(Convert.ChangeType(initialValue, targetType, (IFormatProvider)culture)); } else { if (initialValue is string && typeof(Type).IsAssignableFrom(targetType)) { return((object)Type.GetType((string)initialValue, true)); } if (TypeExtensions.IsInterface(targetType) || TypeExtensions.IsGenericTypeDefinition(targetType) || TypeExtensions.IsAbstract(targetType)) { throw new ArgumentException(StringUtils.FormatWith("Target type {0} is not a value type or a non-abstract class.", (IFormatProvider)CultureInfo.InvariantCulture, (object)targetType), "targetType"); } if (initialValue is string) { if (targetType == typeof(Guid)) { return((object)new Guid((string)initialValue)); } if (targetType == typeof(Uri)) { return((object)new Uri((string)initialValue, UriKind.RelativeOrAbsolute)); } if (targetType == typeof(TimeSpan)) { return((object)TimeSpan.Parse((string)initialValue)); } } TypeConverter converter1 = ConvertUtils.GetConverter(type); if (converter1 != null && converter1.CanConvertTo(targetType)) { return(converter1.ConvertTo((ITypeDescriptorContext)null, culture, initialValue, targetType)); } TypeConverter converter2 = ConvertUtils.GetConverter(targetType); if (converter2 != null && converter2.CanConvertFrom(type)) { return(converter2.ConvertFrom((ITypeDescriptorContext)null, culture, initialValue)); } if (initialValue == DBNull.Value) { if (ReflectionUtils.IsNullable(targetType)) { return(ConvertUtils.EnsureTypeAssignable((object)null, type, targetType)); } else { throw new Exception(StringUtils.FormatWith("Can not convert null {0} into non-nullable {1}.", (IFormatProvider)CultureInfo.InvariantCulture, (object)type, (object)targetType)); } } else if (initialValue is INullable) { return(ConvertUtils.EnsureTypeAssignable(ConvertUtils.ToValue((INullable)initialValue), type, targetType)); } else { throw new InvalidOperationException(StringUtils.FormatWith("Can not convert from {0} to {1}.", (IFormatProvider)CultureInfo.InvariantCulture, (object)type, (object)targetType)); } } }
public static bool IsInstantiatableType(Type t) { ValidationUtils.ArgumentNotNull((object)t, "t"); return(!TypeExtensions.IsAbstract(t) && !TypeExtensions.IsInterface(t) && (!t.IsArray && !TypeExtensions.IsGenericTypeDefinition(t)) && (t != typeof(void) && ReflectionUtils.HasDefaultConstructor(t))); }