views:

178

answers:

2

I have a WinForms combobox to which I bind a list of anonymous objects (printer descriptions and locations).

The goal here is a to select a default printer (which matches printer location).

But within a foreach loop below, I am having trouble accessing the anonymous object's properties.

alt text

I know of a work-around (I tried a private nested DTO with Location & Description property)
but is there a way to access anonymous object's properties without creating a concrete class?

Maybe I was just too lazy here...

+6  A: 

There are, but they're far more hackish. Go with the concrete class.

To name a few:

  1. Reflection
  2. Typing by example

But to be sure, both of the above items will require more coding than it will take to create a simple POCO.

David Morton
+1: For example, reflection. But yeah, that's hackish.
John Gietzen
Thanks, David; Hearing that *Reflection* made me cringe... and ended up implementing it with a simple `POCO` ;) Thanks.
Sung Meister
+2  A: 

If you need to share an object between functions then you need to use a concrete class. Anonymous types are only useful for sharing data within a function or to a binding interface

JaredPar
I think I was trying to be a little bit too clever here...
Sung Meister