Copy Code code as follows:
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Linq.Expressions;
Using System.Text;
Using System.Threading.Tasks;
Namespace Consoleapplication
{
Class Program
{
Class Test
{
public string PropertyJustForTest1 {get; set;}
Public Test PropertyJustForTest2 {get; set;}
}
static void Main (string[] args)
{
Test test = new test ();
Console.WriteLine (getpropertynamehelper.getpropertyname<object> () => test. PROPERTYJUSTFORTEST1));
Console.WriteLine (getpropertynamehelper.getpropertyname<object> () => test. PROPERTYJUSTFORTEST2));
}
}
Static Class Getpropertynamehelper
{
public static string getpropertyname<t> (Expression<func<t>> Express)
{
var memberexpress = Express. Body as memberexpression;
if (memberexpress!= null)
{
return memberExpress.Member.Name;
}
Else
{
return string. Empty;
}
}
}
}