Home >Backend Development >C++ >How Can I Retrieve a Property Name from a Lambda Expression in C#?
Extract the attribute name from the Lambda expression
The initial implementation involved the converting Lambda expression into a member expression, assuming the attribute is a string. However, this is only applicable to the string attributes.
In order to solve this problem, a more comprehensive method is needed, which can handle string and non -string attributes at the same time. The following method provides a solution:
<code class="language-csharp">public static PropertyInfo GetPropertyInfo<TSource, TProperty>( TSource source, Expression<Func<TSource, TProperty>> propertyLambda)</code>This method accepts source objects and lambda expressions as input, and returns the PropertyInfo object that indicates the accessable attributes. If the expression does not quote the attribute, it will cause abnormalities.
In the implementation of the method:
The above is the detailed content of How Can I Retrieve a Property Name from a Lambda Expression in C#?. For more information, please follow other related articles on the PHP Chinese website!