C# property name to string


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; }}


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


Leave a Reply

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