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 IsNullableType(Type t) { ValidationUtils.ArgumentNotNull((object)t, "t"); if (TypeExtensions.IsGenericType(t)) { return(t.GetGenericTypeDefinition() == typeof(Nullable <>)); } else { return(false); } }
private static bool InheritsGenericDefinitionInternal(Type currentType, Type genericClassDefinition, out Type implementingType) { if (TypeExtensions.IsGenericType(currentType)) { Type genericTypeDefinition = currentType.GetGenericTypeDefinition(); if (genericClassDefinition == genericTypeDefinition) { implementingType = currentType; return(true); } } if (TypeExtensions.BaseType(currentType) != null) { return(ReflectionUtils.InheritsGenericDefinitionInternal(TypeExtensions.BaseType(currentType), genericClassDefinition, out implementingType)); } implementingType = (Type)null; return(false); }
private static bool IsOverridenGenericMember(MemberInfo memberInfo, BindingFlags bindingAttr) { switch (TypeExtensions.MemberType(memberInfo)) { case MemberTypes.Field: case MemberTypes.Property: Type declaringType = memberInfo.DeclaringType; if (!TypeExtensions.IsGenericType(declaringType)) { return(false); } Type genericTypeDefinition = declaringType.GetGenericTypeDefinition(); if (genericTypeDefinition == null) { return(false); } MemberInfo[] member = genericTypeDefinition.GetMember(memberInfo.Name, bindingAttr); return(member.Length != 0 && ReflectionUtils.GetMemberUnderlyingType(member[0]).IsGenericParameter); default: throw new ArgumentException("Member must be a field or property."); } }