How to get the Property Name as a string?
For example:
Public int PropertyValue{get;set;}
Now i want to get the PropertyValue as a string with out reflection and with out foreach PropertyInfo
How to get the Property Name as a string?
For example:
Public int PropertyValue{get;set;}
Now i want to get the PropertyValue as a string with out reflection and with out foreach PropertyInfo
I found a solution here: Workaround for lack of 'nameof' operator in C# for type-safe databinding?
Where @reshefm had this code:
class Program
{
static void Main()
{
var propName = Nameof<SampleClass>.Property(e => e.Name);
Console.WriteLine(propName);
}
}
public class Nameof<T>
{
public static string Property<TProp>(Expression<Func<T, TProp>> expression)
{
var body = expression.Body as MemberExpression;
if(body == null)
throw new ArgumentException("'expression' should be a member expression");
return body.Member.Name;
}
}
Hope this helps :)