Get class type for dynamic type?

I wrote the code:

public static object func() { return new { a = 1, b = 2 }; } Console.WriteLine((func() as dynamic).a); //returns '1'. 

If I can do: func() as dynamic , so dynamic should be some kind of reference type / class.

I was looking for its class type, but could not find it (through the reflector).

what is its type? (reference type)?

+4
source share
1 answer

You can get the type through GetType () as usual.

This is an anonymous type, which (as an implementation detail) is a generic type form. A type name is intentionally unpronounceable in C #.

If you look in the reflector, there is probably an internal generic type somewhere ending in 2 (to indicate 2 type parameters), with two properties “a” and “b” of the first and second arguments of the general type, respectively. This is a class, therefore a reference type.

As a note:

 new { a = true, b = 123.45 } 

In fact, the same general type will be used, but with different parameters of the type type.

Also; using dynamic does not change the object - it only changes the way it is accessed.

+6
source

Source: https://habr.com/ru/post/1382929/


All Articles