[英]How to get a property value from a MemberExpression
I am trying to get the property value via a MemberExpression. 我试图通过MemberExpression获取属性值。
For example, given the following object, I want to get the Guid value in the "Id" property. 例如,给定以下对象,我想在“ Id”属性中获取Guid值。
public class Employee
{
public Guid Id {get; set}
}
I have an event being called which has a MemberExpression passed to that event. 我有一个事件被调用,其中有一个MemberExpression传递给该事件。 The MemberExpression parameter represents the Employee.Id property.
MemberExpression参数表示Employee.Id属性。 How can I get the VALUE of "Id" from the MemberExpression?
我怎样才能获得“ID”从MemberExpression 价值 ? The code I'm trying to use is as follows:
我尝试使用的代码如下:
(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
}
at this point you have 2 properties to make you double indirection. 此时,您具有2个属性,使您可以进行两次间接定向。
it miss a entry point : a reference to "BusinessObjects" 它错过了一个切入点:对“ BusinessObjects”的引用
then call 然后打电话
var businessObjects = ???
var id = employeeIdProperty.GetValue(parentObject.GetValue(businessObjects));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.