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

后端 未结 12 1195
南旧
南旧 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:17

    You could use the dynamic type to access properties of anonymous types at runtime without using reflection.

    var aType = new { id = 1, name = "Hello World!" };
    //...
    //...
    dynamic x = aType;
    Console.WriteLine(x.name); // Produces: Hello World!
    

    Read more about dynamic type here: http://msdn.microsoft.com/en-us/library/dd264736.aspx

提交回复
热议问题