how to create expression tree / lambda for a deep property from a string

后端 未结 5 1283
渐次进展
渐次进展 2020-12-13 16:07

Given a string: \"Person.Address.Postcode\" I want to be able to get/set this postcode property on an instance of Person. How can I do this? My idea was to split the string

5条回答
  •  萌比男神i
    2020-12-13 16:42

    If anyone is interested in the performance trade-off between the simple reflection approach (also nice examples here and here) and Marc's Expression-building approach...

    My test involved getting a relatively deep property (A.B.C.D.E) 10,000 times.

    1. Simple reflection: 64 ms
    2. Expression-building: 1684 ms

    Obviously this is a very specific test, and I haven't considered optimisations or setting properties, but I think a 26x performance hit is worth noting.

提交回复
热议问题