GeneralUtils.cs 527 B

123456789101112131415
  1. namespace MeloongCore;
  2. public static class GeneralUtils {
  3. /// <summary>
  4. /// 判断对象是否为指定泛型类型的实例。
  5. /// </summary>
  6. public static bool IsGenericInstanceOf(object? instance, Type genericTypeDefinition) {
  7. if (instance is null) return false;
  8. for (var type = instance.GetType(); type is not null; type = type.BaseType) {
  9. if (type.IsGenericType && type.GetGenericTypeDefinition() == genericTypeDefinition) return true;
  10. }
  11. return false;
  12. }
  13. }