开发者

How to access anonymous object's property

开发者 https://www.devze.com 2022-12-21 19:11 出处:网络
I have a WinForms combobox to which I bind a list of anonymous objects (printer descriptio开发者_运维百科ns and locations).

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.

How to access anonymous object's property

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:

  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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消