Scope might not be the right word. With the following code I can\'t get access to the collections element\'s object\'s properties. Is there a better return data type or a way t
Once you leave the scope in which the anonymous type was defined, you have to use reflection to get to its members.
The two other options I can think of are to use a dynamic type (if using 4.0 framework or later) or to create a defined type instead of an anonymous type.
There are two feasible solutions:
dynamic
instead of object
. The problem with this is that you lose compile time checking of the code that uses the result of this method.Use IEnumerable<dynamic>
instead.
According to msdn "The dynamic type enables the operations in which it occurs to bypass compile-time type checking. Instead, these operations are resolved at run time. The dynamic type simplifies access to COM APIs such as the Office Automation APIs, and also to dynamic APIs such as IronPython libraries, and to the HTML Document Object Model (DOM)."
http://msdn.microsoft.com/en-us/library/dd264741.aspx
I would recommend simply creating a new type for passing the data. If the scope of the data is protected, you can used a protected nested subclass to prevent over-proliferation of small data classes.