I have a method which returns as follows,
return new { a, b, c, d};
And method definition is public object GetValues();
How do I access those variables a, b, c, d after calling this method?
Two options:
Use dynamic typing, so long as you're using it from the same assembly:
dynamic values = GetValues();
var a = values.a; //etc
Use reflection directly; the generated type will have public read-only properties called a
, b
, c
and d
Alternatively, if you can possibly change the method to not use an anonymous type, do so. (You may not be able to change the signature of the method depending on the context, but even then you could still cast in the calling code.)
See more on this question at Stackoverflow