I have this code:
object test = new {a = "3", b = "4"};
Console.WriteLine(test); //I put a breakpoint here
How can I access a
property of test
object? When I put a breakpoint, visual studio can see the variables of this object... Why can't I? I really need to access those.
If you cannot use static typing for your anonymous class, you can use
dynamic
, like this:The downside to this approach is that the compiler is not going to help you detect situations when a property is not defined. For example, you could write this
and it would compile, but it would crash at runtime.