How to access anonymous object's property
I have a WinForms combobox to which I bind a list of anonymous objects (printer descriptio开发者_运维百科ns 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.
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...
There are, but they're far more hackish. Go with the concrete class.
To name a few:
- Reflection
- 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.
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
精彩评论