I’m in a situation where I need to return an object of an anonymous type from a method, is it a good idea to use dynamic as a return type? what considerations to take?
public dynamic MyMethod()
{
// process and return the object of an anonymous type
}
Yes it has sense If you “guarantee” that you’ll always return an object with some characteristics , with an
Idfor example (ignoring that perhaps it would be better to use an Interface)So if you guarantee that all your objects can
Turn Left-Rightbut you don’t guarantee if they are airplanes, cars, motos, boats. (so it’s good if you are doing Duck typingWhen I see a bird that walks like a duck and swims like a duck and quacks like a duck, I call that bird a duck.)Note that if you then need to reflect on your objects, it can become ugly: How do I reflect over the members of dynamic object?