Tagged as

Return Anonymous type

In this Article I am going to discuss about returning the anonymous type and how to handle in code. Following is list of fact about anonymous type.

Quick facts about Anonymous type

Anonymous types are reference type derived form system.objects.

Properties of the Anonymous type is read only.

If two Anonymous type has same properties and same order than compiler treats its as same type. But if both are in one assembly.

Anonymous type has method scope. If you want to return Anonymous type form the method than you have to convert it in object type. But is not good practice.

You can read more on blog about this: Anonymous types
As in about facts you cannot return anonymous type from the method , if you want to return you need to cast it in object.
Now in following post I am going to do same return the anonymous type as object and going to show three different way to handle it.

Way 1: Handle using Dynamic type

Way 2: Handle by creating same anonymous type

Way 3: Handle using Reflection

To Understand each way I created following method which returns anonymous type

As you see in above example first line of code calling method which is returning anonymous type as object and assign the return value to dynamic type. Second line of code just printing the property value of anonymous type.
Note : No intellisense support as we are using dynamic type. And need to remember the property name and type also.

This way making use of reflection feature of .net. First line of code call the method and assign return value to refobj. Second line of code get the Type of the object and than following line of code get the property of anonymous type and print value of it.Check out full Source code of to test all technique we discuss

Comments and Discussions

I am only voting three because I considered it interesting to see the use of the dynamic keyword and even the creation of that awkward Cast method, showing that the object is really of the same type.

But aside from interesting, those are all really bad practices.
Returning an anonymous type? Well, if the receiver is going to use reflection to display it, OK... for all the other cases, don't do it!

Using dynamic to access that type?
Well, even if you don't create a full class, the compiler is creating a full class for you, but instead of using that class directly, you are going to a dynamic approach, which will not support refactoring and will not generate compile time errors if you write the wrong property name.

And the cast one... hey, if you KNOW that it is the same type as to write a "new" with the same property names and fill it with the same value types (even if you used an empty string, it is still a string), then why not lose time creating a type?
Also, this solution will not support either refactoring and, worst, if you simple add a new property on the method that returns the anonymous object, all the places that used that cast will fail!

Edit: I corrected good practices by bad practices... I may be thinking they are not good practices and I wrote only good...