You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Connected.Framework/Connected.Interop/Generics.cs

51 lines
994 B

namespace Connected.Interop
{
public static class Generics
{
public static bool IsSubclassOfGenericType(this Type type, Type genericType)
{
var current = type.BaseType;
while (current is not null && current != typeof(object))
{
var currentType = current.IsGenericType ? current.GetGenericTypeDefinition() : current;
if (genericType == currentType)
return true;
current = current.BaseType;
}
return false;
}
public static bool ImplementsInterface(this Type type, Type interfaceType)
{
var interfaces = type.GetInterfaces();
foreach (var i in interfaces)
{
if (!i.IsGenericType)
{
if (i == interfaceType)
return true;
continue;
}
var definition = i.GetGenericTypeDefinition();
if (definition == interfaceType)
return true;
}
return false;
}
public static bool ImplementsInterface<TInterface>(this Type type)
{
return ImplementsInterface(type, typeof(TInterface));
}
}
}