1

Possible Duplicate:
Anonymous Types - Are there any distingushing characteristics?

Can't find suitable property.

if(new {a = 2, b= "z"}.GetType()...)

what to put instead of ...?

Community
  • 1
  • 1
Andrej Slivko
  • 1,236
  • 2
  • 12
  • 27
  • Welcome to SO, please don't forget to visit http://stackoverflow.com/faq – Reigel Gallarde Jun 16 '10 at 06:42
  • As far as i understood there is no out of the box solution, all answers provided in this question and in http://stackoverflow.com/questions/315146/anonymous-types-are-there-any-distingushing-characteristics sounds more like workarounds that could stop working with different/new compiler version. It would be nice to have something like if(new {a = 2, b= "z"}.GetType().IsAnonymousType) – Andrej Slivko Jun 16 '10 at 10:27

3 Answers3

2

Except for the weird name starting with <> and containing AnonymousType (in C#, as in VB it starts with VB$) there's not much to be tested. I wouldn't bet on name testing, however...

Sorin Comanescu
  • 4,829
  • 3
  • 29
  • 38
-1

Have you tried outputting new { a = 2, b = "z" }.GetType() to get a value to compare with? If not, this is what I'd do first.

var t = new { a = 2, b = "z" }.GetType();

var c = 2; // set a breakpoint on this line, and see what t contains
Tomas Aschan
  • 58,548
  • 56
  • 243
  • 402
-1

Anonymous class will include the name AnonymousType, they wont have a namespace or a declaring type. You might use that to see if it's anonymous. Although I'm not sure how safe it is...

var t = new { a = 2, b = "z" }.GetType();
bool isAnonymous = t.Namespace == null && t.DeclaringType == null;
simendsjo
  • 4,739
  • 2
  • 25
  • 53