0
我試圖通過MemberExpression獲取屬性值。如何從MemberExpression獲取屬性值
例如,給定以下對象,我想要在「Id」屬性中獲取Guid值。
public class Employee
{
public Guid Id {get; set}
}
我有一個事件被調用,其中有一個MemberExpression傳遞給該事件。 MemberExpression參數表示Employee.Id屬性。我如何從MemberExpression獲得「Id」的VALUE?我試圖使用的代碼如下:
(MemberExpression employeeIdMember is parameter to the event)
if ((employeeIdMember.Member as PropertyInfo) != null)
{
PropertyInfo employeeIdProperty = employeeIdMember.Member as PropertyInfo;
// at this point employeeIdProperty represents {System.Guid Id}
PropertyInfo parentObject = (MemberExpression)employeeIdMember.Expression).Member as PropertyInfo;
// at this point, parentObject represents {BusinessObjects.Employee Employee}
// HOW to call employeeIdProperty.GetValue(parentObject) to get the Id Property Value?? I've tried this call here, but it does not work
}