In C# 3.0, is it possible to determine whether an instance of Type
represents an Anonymous Type?
6 Answers
Even though an anonymous type is an ordinary type, you can use some heuristics:
public static class TypeExtension {
public static Boolean IsAnonymousType(this Type type) {
Boolean hasCompilerGeneratedAttribute = type.GetCustomAttributes(typeof(CompilerGeneratedAttribute), false).Count() > 0;
Boolean nameContainsAnonymousType = type.FullName.Contains("AnonymousType");
Boolean isAnonymousType = hasCompilerGeneratedAttribute && nameContainsAnonymousType;
return isAnonymousType;
}
}
Another good heuristic to be used is if the class name is a valid C# name (anonymous type are generated with no valid C# class names - use regular expression for this).

- 666
- 5
- 8
-
-
1
-
2
-
FYI - this doesn't work if you're checking the type from within a generic class or function – Thomas Johnson Jul 19 '17 at 15:37
-
Clarification: this doesn't work if you're checking a type param in a generic class/method. check the actual instance of an object's .GetType() result instead – Thomas Johnson Jul 19 '17 at 15:53
Properties of an anonymous typed object
- has a namespace equal to null
- base type of System.Object
- IsSealed = true
- custom attribute 0 is DebuggerDisplayAttribute, Type: ""
- IsPublic = false
For my particular application, if the namespace is null it can be inferred that the type is anonymous, so checking that the namespace is null is probably the least expensive check.

- 9,605
- 6
- 67
- 94
-
2Thanks, opting for the return type.NameSpace == null; improved my programs' processing by almost 50% (was using this approach: http://stackoverflow.com/questions/2483023/how-to-test-if-a-type-is-anonymous) – Schalk Oct 06 '11 at 12:06
-
Yeah. This should be the correct answer. I don't think any reasonable concrete class would ever have a null namespace. – Christopher Davies Feb 02 '13 at 07:17
-
+1 thanks! Leaving here my comment in order to favorite this answer (not just a thread) – Gennady Vanin Геннадий Ванин Feb 19 '13 at 08:24
-
I think global classes can have namespace == null; so that check alone wont be enough. @Schalk – nawfal Oct 10 '13 at 07:17
-
@nawfal C# has no concept (VB.NET does, as do possibly more, but if restricting to a language). – user2864740 Nov 07 '16 at 06:01
-
@user2864740 C# does have the concept of null namespace classes. Try a class without namespace around it.. – nawfal Nov 09 '16 at 12:58
-
Dont you think you can perfectly have all these conditions met for a named type as well? This answer looks incorrect. – nawfal Nov 09 '16 at 13:02
There is no C# language construct which allows you to say "Is this an anonymous type". You can use a simple heuristic to approximate if a type is an anonymous type, but it's possible to get tricked by people hand coding IL, or using a language where such characters as > and < are valid in identifiers.
public static class TypeExtensions {
public static bool IsAnonymousType(this Type t) {
var name = t.Name;
if ( name.Length < 3 ) {
return false;
}
return name[0] == '<'
&& name[1] == '>'
&& name.IndexOf("AnonymousType", StringComparison.Ordinal) > 0;
}

- 733,204
- 149
- 1,241
- 1,454
In methadata and CLR there is no such terms as anonymous types. Anonymous types are solely compiler feature.

- 5,221
- 2
- 18
- 25
Might be helpful to know why you want to know this. If you execute the following:
var myType = new { Name = "Bill" };
Console.Write( myType.GetType().Name );
...you would see something like "<>f__AnonymousType0`1" output as the type name. Depending on your requirements, you may be able to assume that a type starting with <>, containing "AnonymousType" and a back quote character is what you're looking for.

- 10,491
- 1
- 35
- 58
-
-
I thought the same thing, but it's a bit dirty. What if they change the name in c#5? Any code that uses it will be broken. – Philip Wallace Oct 30 '09 at 16:22
-
It's important to ask and explain "why" 'cause often there are other possible answers which may not be evident from the question without knowing more. – Samuel Neff Oct 30 '09 at 16:25
It seems anonymous types get a DebuggerDisplayAttribute
put on them where Type = "<Anonymous Type>"
.
Edit: But only when you compile in Debug mode. Darn.

- 11,097
- 6
- 35
- 36