Anonymous types. Are there any distinguishing characteristics?

Is there anything to use to determine if a type is actually an anonymous type? For example, an interface, etc.

The goal is to create something like the following ...

//defined like... public static T Get<T>(this IAnonymous obj, string prop) { return (T)obj.GetType().GetProperty(prop).GetValue(obj, null); } //... //And then used like... var something = new { name = "John", age = 25 }; int age = something.Get<int>("age"); 

Or is it just anonymous type beauty? You cannot identify him yourself, because he takes a new form?

Note I understand that you can write an extension method for the object class, but, in my opinion, this seems a little redundant.

+22
generics c # anonymous-types extension-methods
Nov 24 '08 at 19:21
source share
3 answers

EDIT: The list below is for anonymous C # types. VB.NET has different rules - in particular, it can generate mutable anonymous types (and does by default). Jared noted in a comment that the naming style is also different. Basically it's all pretty fragile ...

You cannot identify it in a general restriction, but:

  • It will be a class (not an interface, enum, struct, etc.).
  • It will use CompilerGeneratedAttribute
  • It will override Equals, GetHashCode and ToString.
  • It will be in the global namespace
  • It will not be nested in another type.
  • He will be internal
  • It will be sealed.
  • It will be output directly from object
  • It will be common with as many type parameters as properties. (You may have a non-trivial anonymous type with no properties. This is a bit pointless.)
  • Each property will have a type parameter with a name including the name of the property, and will have this type parameter, for example. the Name property becomes a property of type <> _ Name
  • Each property will be public and read-only.
  • For each property there will be a corresponding readonly private field
  • There will be no other properties or fields.
  • There will be a constructor that takes one parameter corresponding to each type parameter, in the same order as the type parameters
  • Each method and property will use DebuggerHiddenAttribute .
  • The type name will begin with "<>" and will contain "Anonymous Type"

However, very little of this is guaranteed by the specification - so that all of them may change in the next version of the compiler, or if you use Mono, etc.

+48
Nov 24 '08 at 19:38
source share

As far as I remember, there is a [CompilerGenerated] marker ... 2 sec.

Plus the name will be fancy, and it will be a generic type; -p

Actually, for "get", etc. I would probably just use the static (non-expanding) method.

If you just need a way to get the value from an anon-type instance (at a later point in time), perhaps the best option is lambda - note that you need a few tricks to remove this:

  static void Main() { var foo = new { name = "John", age = 25 }; var func = Get(foo, x => x.age); var bar = new { name = "Marc", age = 30 }; int age = func(bar); } // template here is just for type inference... static Func<TSource, TValue> Get<TSource, TValue>( TSource template, Func<TSource, TValue> lambda) { return lambda; } 

(edit the comment). Definitely this attribute:

  var foo = new { A = "B" }; Type type = foo.GetType(); CompilerGeneratedAttribute attrib = (CompilerGeneratedAttribute) Attribute.GetCustomAttribute( type, typeof(CompilerGeneratedAttribute)); // non-null, therefore is compiler-generated 
+4
Nov 24 '08 at 19:23
source share

For the purpose of extension methods, it is not possible to distinguish an anonymous type. Extension methods work by specifying a method for the compile time type. Anonymous types are undetectable and therefore not displayed at compile time. This makes them incompatible with extension methods.

+1
Nov 24 '08 at 19:24
source share



All Articles