This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Linq.Expressions; | |
/// <summary> | |
/// Returns the name of the specified property of the specified type. | |
/// </summary> | |
/// <typeparam name="T">The type the property is a member of.</typeparam> | |
/// <param name="property">The property expression.</param> | |
/// <returns>The property name.</returns> | |
public static string GetPropertyName<T>(Expression<Func<T, object>> property) | |
{ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void Main() | |
{ | |
// from comment discussion http://stackoverflow.com/questions/671968/retrieving-property-name-from-lambda-expression/17220748#17220748 | |
Expression<Func<O, int>> exprId = o => o.Id; | |
Expression<Func<O, string>> exprName = o => o.Name; | |
Expression<Func<O, int[]>> exprItems = o => o.Items; | |
Expression<Func<O, int>> exprItemsLength = o => o.Items.Length; | |
Expression<Func<O, Subclass>> exprChild = o => o.Child; | |
Expression<Func<O, string>> exprChildName = o => o.Child.Name; |