How can I get a value of a property from an anonymous type?

后端 未结 12 1256
南旧
南旧 2020-12-24 08:34

I have a datagrid populated by a Linq query. When the focused row in the datagrid changes I need to set a variable equal to one of the properties in that object.

I t

12条回答
  •  攒了一身酷
    2020-12-24 09:11

    If you know what you are doing and are not afraid of getting runtime errors when your code changes, you could cast your row data as dynamic:

    var data = view.GetRow(rowHandle) as dynamic;  
    
    int clientId      = data.ClientID;
    string clientName = data.ClientName;
    int jobs          = data.Jobs
    

    No Compile-time verification. But it should work nicely.

提交回复
热议问题