How to check if a type is anonymous?

I have the following method that serializes an object into an HTML tag. I only want to do this, though if the type is not anonymous.

private void MergeTypeDataToTag(object typeData) { if (typeData != null) { Type elementType = typeData.GetType(); if (/* elementType != AnonymousType */) { _tag.Attributes.Add("class", elementType.Name); } // do some more stuff } } 

Can someone show me how to do this?

thank

+58
reflection c # anonymous-types
Mar 20 '10 at 12:44
source share
5 answers

From http://www.liensberger.it/web/blog/?p=191 :

 private static bool CheckIfAnonymousType(Type type) { if (type == null) throw new ArgumentNullException("type"); // HACK: The only way to detect anonymous types right now. return Attribute.IsDefined(type, typeof(CompilerGeneratedAttribute), false) && type.IsGenericType && type.Name.Contains("AnonymousType") && (type.Name.StartsWith("<>") || type.Name.StartsWith("VB$")) && type.Attributes.HasFlag(TypeAttributes.NotPublic); } 

EDIT:
Another link with extension method: determining if a type is an anonymous type

+59
Mar 20 '10 at 12:50
source share

Quick and dirty:

 if(obj.GetType().Name.Contains("AnonymousType")) 
+14
Feb 22 '12 at 11:51
source share

You can simply check if the namespace is null.

 public static bool IsAnonymousType(this object instance) { if (instance==null) return false; return instance.GetType().Namespace == null; } 
+9
Mar 07 '13 at 14:00
source share

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) { //WOW! We have an anonymous type!!! return true; } } } return false; } public static bool IsAnonymousType<T>(this T instance) { return IsAnonymous(instance.GetType()); } } 
+7
Jul 13 2018-12-14T00:
source share

Check out CompilerGeneratedAttribute and DebuggerDisplayAttribute.Type

here is the code generated by the compiler for an anonymous type

 [CompilerGenerated, DebuggerDisplay(@"\{ a = {a} }", Type="<Anonymous Type>")] internal sealed class <>f__AnonymousType0<<a>j__TPar> { ... } 
+5
Mar 20 '10 at 12:49
source share



All Articles