Kawałek kodu który może się przydać:
Code:
public static string GetPropertyName<T>(Expression<Func<T, object>> expression) { return expression.Body as MemberExpression != null ? ((MemberExpression) expression.Body).Member.Name : ((MemberExpression) ((UnaryExpression) expression.Body).Operand).Member.Name; }
Użycie:
Code:
class Program { static void Main(string[] args) { Console.WriteLine(GetPropertyName<Person>(person => person.FirstName)); Console.WriteLine(GetPropertyName<Person>(person => person.LastName)); Console.WriteLine(GetPropertyName<Person>(person => person.Age)); } public static string GetPropertyName<T>(Expression<Func<T, object>> expression) { return expression.Body as MemberExpression != null ? ((MemberExpression) expression.Body).Member.Name : ((MemberExpression) ((UnaryExpression) expression.Body).Operand).Member.Name; } } public class Person { public string FirstName { get; set; } public string LastName { get; set; } public int Age { get; set; } }
Brak komentarzy:
Prześlij komentarz