1 頁 (共 1 頁)

如何取得 PropertyName

發表於 : 週三 10月 31, 2012 10:20 am
rusli
[syntax="csharp"]

using System;
using System.Linq.Expressions;

public class Program
{
public static string GetPropertyName<T>(Expression<Func<T>> myExpression)
{
return (myExpression.Body as MemberExpression).Member.Name;
}

void Main()
{
MyClass A = new MyClass();
string propertyName = GetPropertyName(() => A.MyProperty) // return "MyProperty"
}
}

public class MyClass
{
public string MyProperty { get; set; }
}

[/syntax]