C# property name to string

Code

public class Util {    static string GetPropertyName<T>(System.Linq.Expressions.Expression<Func<T>> propertyExpression)    {        return (propertyExpression.Body as System.Linq.Expressions.MemberExpression).Member.Name;    }}public class PageItem{    public int Id { get; set; }    public string Title { get; set; }    public string Content { get; set; }    public string FriendlyUrl { get; set; }    public string ControllerName { get; set; }}

Usage

PageItem page = new PageItem();Response.Write(Util.GetPropertyName(() => page.Title);

 

Leave a Reply

Your email address will not be published. Required fields are marked *