I need to validate an object to see whether it is null, a value type, or IEnumerable
where T
is a value type. So far I have:
My generic contribution that checks if a given type (or its base classes) implements an interface of type T:
public static bool ImplementsInterface(this Type type, Type interfaceType)
{
while (type != null && type != typeof(object))
{
if (type.GetInterfaces().Any(@interface =>
@interface.IsGenericType
&& @interface.GetGenericTypeDefinition() == interfaceType))
{
return true;
}
type = type.BaseType;
}
return false;
}