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? }
Using an expression, you can:
static PropertyInfo ExtractProperty<T>(Expression<Func<T>> selector) { return (selector.Body as MemberExpression).Member as PropertyInfo; }
Source: https://habr.com/ru/post/1315243/More articles:Blank black screen when starting iPad application - objective-cHow to be ready Python 3? - coding-styleConvert VB to C # or VB.net - .netShould I show a field with a null value or a HasFoo field? - designDoctrine: changing the date format when getting and setting field values - phpWhich of `if x:` or `if x! = 0:` is preferred in Python? - pythonHelp me translate Python code that replaces the extension in the file name in C ++ - c ++Is there a SQL Server function to display pseudo-random database entries? - c #Zend DB and coding - phpAppendix C command line: how to programmatically move the cursor - cAll Articles