1using System.Reflection;
 
    2using System.Runtime.CompilerServices;
 
    6internal static class TypeExtensions
 
    8    public static bool IsClosureType(
this Type ty)
 
   10        var compilerGen = ty.GetCustomAttribute<CompilerGeneratedAttribute>() != 
null;
 
   13        var dcName = ty.Name.StartsWith(
"<>c__DisplayClass");
 
   15        return compilerGen && dcName;
 
   18    public static Type? GetGenInst(
this Type ty, Type genTy)
 
   20        if (!genTy.IsGenericTypeDefinition)
 
   22            throw new ArgumentException($
"{nameof(genTy)} is not a generic type definition.");
 
   25        if (genTy.IsInterface)
 
   27            foreach (var iface 
in ty.GetInterfaces())
 
   29                if (iface.IsGenericType && iface.GetGenericTypeDefinition() == genTy)
 
   39            while (curr is not 
null)
 
   41                if (curr.IsGenericType && curr.GetGenericTypeDefinition() == genTy)