Generally speaking, I'm fairly opposed to the use of anonymous types in real-world code (except in LINQ and some other specific scenarios), but I'm looking right now at a situation where I need to bind an ASP GridView
control with a collection of tuple values and they're looking pretty tempting.
Basically, I need to show a list of proprietary accounts that are associated with different computer names. I don't currently have a type that contains those two properties because I don't use them together like this anywhere else.
What I would normally do in a situation like this is just create a new class that has the properties, set the ItemType
on the GridView
to reference that type, then build objects of that type in my Select
method (I use ObjectDataSource
much of the time). But I'm thinking now that there's really no benefit to throwing in that extra class, since no matter what it's getting bound via reflection.
Is there any reason to shy away from using anonymous types in data-binding situations like this?
I'd accept anything: performance, readability, maintainability, principle, or whatever else you can throw at me. But the only disadvantage I can think of is if I wanted to use the Item
property to use Intellisense for my data-binding, but in this particular case I have no use for anything more than BoundField
s. Of course, this is all based on the assumption that it even works, admittedly I haven't tried it yet with anonymous types, so it might just not be supported for one reason or another.
Best Answer
No
And your assumption about it not being statically typed is wrong too.
From the documentation: