PropertyInfo from delegate

Is there an easy way to get PropertyInfo for a property in a delegate if it is the simplest property of properties?

Example:

var propertyInfo = Method<MyClass,int>(s => s.Property); ... PropertyInfo Method(Func<T1,T2> selector) { // What goes here? } 
+2
source share
1 answer

Using an expression, you can:

  static PropertyInfo ExtractProperty<T>(Expression<Func<T>> selector) { return (selector.Body as MemberExpression).Member as PropertyInfo; } 
+8
source

Source: https://habr.com/ru/post/1315243/


All Articles