public static TypeContainer LookupGenericTypeContainer (Type t)
{
- while (t.IsGenericInstance)
- t = t.GetGenericTypeDefinition ();
-
+ t = DropGenericTypeArguments (t);
return LookupTypeContainer (t);
}
return t.GetGenericArguments ();
}
+ public static Type DropGenericTypeArguments (Type t)
+ {
+ if (!t.IsGenericType)
+ return t;
+ // Micro-optimization: a generic typebuilder is always a generic type definition
+ if (t is TypeBuilder)
+ return t;
+ return t.GetGenericTypeDefinition ();
+ }
+
//
// Whether `array' is an array of T and `enumerator' is `IEnumerable<T>'.
// For instance "string[]" -> "IEnumerable<string>".
int tcount = GetNumberOfTypeArguments (type);
int pcount = GetNumberOfTypeArguments (parent);
- if (type.IsGenericInstance)
- type = type.GetGenericTypeDefinition ();
- if (parent.IsGenericInstance)
- parent = parent.GetGenericTypeDefinition ();
-
if (tcount != pcount)
return false;
+ type = DropGenericTypeArguments (type);
+ parent = DropGenericTypeArguments (parent);
+
return type.Equals (parent);
}
public static bool IsNullableType (Type t)
{
- if (!t.IsGenericInstance)
- return false;
-
- Type gt = t.GetGenericTypeDefinition ();
- return gt == generic_nullable_type;
+ return generic_nullable_type == DropGenericTypeArguments (t);
}
}