In this post we are going to see how to get the name of the Property of a class using C#, First we have to create a template class Employee, Then we have to create a static class to get the name of the property
output:
From this post you can learn how to get the name of the Property of a class in C#
public class PropExtension
{
public static string GetPropName<T, V>(Expression<Func<T, V>> expression)
{
var _dat = (MemberExpression)expression.Body;
return _dat.Member.Name;
}
}
public class Employee
{
public string Name { set; get; }
public string Role { set; get; }
}
static void Main(string[] args)
{
var propname = PropExtension.GetPropName((Employee e) => e.Role);
Console.WriteLine(propname);
Console.Read();
}
output:
From this post you can learn how to get the name of the Property of a class in C#