add TypeExtensions to support Type.IsSubclassOfGeneric(...)

This commit is contained in:
Michael Becker 2024-07-21 22:38:38 -04:00
parent 623402805b
commit 6ee1135732
Signed by: beckermj
GPG Key ID: 24F8DAA73DCB2C8F

View File

@ -0,0 +1,32 @@
namespace MBS.Core.Reflection;
public static class TypeExtensions
{
public static bool IsSubclassOfGeneric(this Type toCheck, Type generic)
{
// thanks https://stackoverflow.com/a/457708
while (toCheck != null && toCheck != typeof(object))
{
var cur = toCheck.IsGenericType ? toCheck.GetGenericTypeDefinition() : toCheck;
if (generic == cur)
{
return true;
}
Type[] intfs = toCheck.GetInterfaces();
foreach (Type intf in intfs)
{
// !!! HACK HACK HACK !!!
bool hack = intf.Namespace.Equals(generic.Namespace) && intf.Name.Equals(generic.Name);
if (hack)
{
return true;
}
}
toCheck = toCheck.BaseType;
}
return false;
}
}