Way to detect anonymous type programmatically - by Christian Liensberger - INACTIVE

Status : 

  Won't Fix<br /><br />
		Due to several factors the product team decided to focus its efforts on other items.<br /><br />
		A more detailed explanation for the resolution of this particular item may have been provided in the comments section.

Sign in
to vote
ID 296989 Comments
Status Closed Workarounds
Type Bug Repros 0
Opened 9/6/2007 9:18:29 AM
Access Restriction Public


There is no way to detect an anonymous type programmatically. I need to check via reflection a list of types and to understand if that type is an anonymous type or another type. There seems to be no property to check or similar.

The only way to understand is the name (which is compiler generated) and the CompilerGeneratedAttribute on the class. But that is not a 100% proof to be an anonymous type!
Sign in to post a comment.
Posted by Jeswin on 9/9/2009 at 12:30 AM
Though this is closed, I am adding comments to support the idea of having a way to identify anonymous types.

Here is my situation:
In our framework (agilefx.codeplex.com), we are analyzing LINQ expressions, which may refer anonymous types and user created types. A code generator creates supporting code for user created types (based on Entity Framework EDMX, which is XML), but it cannot process types generated by the compiler. Such types need to be identified and processed at run-time.

As we see more automation through code generation and expressions, such issues will become more common.
Posted by jhandel on 7/9/2009 at 6:45 AM
It wouldn't be much effort to append an attribute to anonymous types to let us know that they or (or other developers know to treat them as if they were).. In the specific case I have right now, I am extending an API (that I don't have control over) that uses signitures like method(int x, object[] params).. That signiture consumes all overload signitures that start with int. My current plan is to inherit, override, detect anonymous type, process, and then call down to the base. But with no way to detect the anonymous type I'm kinda SOL.. And thats just 1 case off the top of my head. As these types become more pervasive we will see more cases where detection will become useful to the developer.
Posted by Microsoft on 2/8/2008 at 1:25 AM
Hey littleguru,

Anonymous types are intended to be identical to user-created types in every way, so hopefully your architecture should be able to treat them in the same way. Note that users who write LINQ queries may end their query with a select clause that creates custom instances of a named type instead of an anonymous type, filling it with data in an object initializer the same way as you would fill an anonymously-typed object.

Are you certain that your architecture requires you distinguish between anonymous and named types?

Alex Turner
Program Manager
Visual C# Compiler
Posted by Christian Liensberger - INACTIVE on 12/7/2007 at 4:04 AM
Well, I'm having a framework here were I want to allow people to use LINQ and anonymous types. But I need to fill these types with data. For that I need a way to understand if the result type of the LINQ expression or even the type in general is an anonymous type! That's why I'm in search for a way to understand that the type is an anonymous type!
Posted by Microsoft on 12/7/2007 at 3:06 AM
Thanks for the suggestion for Visual Studio 2008!

You are right that it is not possible through reflection to guarantee whether a type you are looking at is anonymous or not conclusively (you should not count on the format of the compiler-generated name as this may change in future compilers).

What is the scenario you're having in which you need to distinguish between anonymous and nominal types?


Alex Turner
Program Manager
Visual C# Compiler
Posted by Microsoft on 9/6/2007 at 5:52 PM
Thanks for your feedback. We are sending this issue to the appropriate group within the Visual Studio Product Team for triage and resolution. Thank you, Visual Studio Product Team.
Posted by Microsoft on 9/6/2007 at 5:39 PM
Thank you for your feedback. We are currently investigating. If this issue is urgent, please call support directly (see http://support.microsoft.com). Thank you, Visual Studio Product Team.