繁体   English   中英

如何从MemberExpression获取属性值

[英]How to get a property value from a MemberExpression

我试图通过MemberExpression获取属性值。

例如,给定以下对象,我想在“ Id”属性中获取Guid值。

public class Employee
{
    public Guid Id {get; set}
}

我有一个事件被调用,其中有一个MemberExpression传递给该事件。 MemberExpression参数表示Employee.Id属性。 我怎样才能获得“ID”从MemberExpression 价值 我尝试使用的代码如下:

(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
}

此时,您具有2个属性,使您可以进行两次间接定向。

它错过了一个切入点:对“ BusinessObjects”的引用

然后打电话

var businessObjects = ???
var id = employeeIdProperty.GetValue(parentObject.GetValue(businessObjects));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM