Friday, April 20, 2012

Quick Tip: C# Strongly Typed Property Name

A quick and easy way to use a type safe (compiler-aware) lambda expresssion

public string GetPropertyName<TSource, TResult>(Expression<Func<TSource, TResult>> propertyExpression)


var memberExpression = propertyExpression.Body as MemberExpression;

return memberExpression != null ? memberExpression.Member.Name : null;


Then call

GetPropertyName<LocalGovernment, string>(x => x.LocalGovernmentName)

Originally found here:

No comments:

Post a Comment