Well, today the compiler generates anonymous types as public and private classes. A paradoxical combination, since the specialization of the clan class is a kind of inheritance, isn't it? So you can check it out: 1. Is this a generic type? Yes => 2) is its definition hermetic && not publicly? Yes => 3) is its definition an attribute of CompilerGeneratedAttribute? I think if these three criteria are true together, we have an anonymous type ... Well ... There is a problem with ANY of the methods described - these are usage aspects that may change in future versions of .NET, and will be so until until Microsoft adds the IsAnonymous boolean property for the Type class. I hope this happens before we all die ... Until this day, you can check it like this:
using System.Runtime.CompilerServices; using System.Reflection; public static class AnonymousTypesSupport { public static bool IsAnonymous(this Type type) { if (type.IsGenericType) { var d = type.GetGenericTypeDefinition(); if (d.IsClass && d.IsSealed && d.Attributes.HasFlag(TypeAttributes.NotPublic)) { var attributes = d.GetCustomAttributes(typeof(CompilerGeneratedAttribute), false); if (attributes != null && attributes.Length > 0) {
Konstantin Isaev Jul 13 2018-12-14T00: 00Z
source share