在哪里可以找到GetMemberInfo

时间:2014-01-06 17:21:37

标签: c# wpf

我已经到处查找了这个内容,我已经为DLL下载了LINQ Data,并且在引用中添加了System.Reflection,但它仍然无法找到它。

C#代码:

protected void NotifyOfPropertyChanged(string name)
{
      PropertyChangedEventHandler handler = PropertyChanged;
      if (handler != null)
      {
          handler(this, new PropertyChangedEventArgs(name));
      }
}

protected void NotifyOfPropertyChanged<TProperty>(Expression<Func<TProperty>> property)
{
      NotifyOfPropertyChanged(property.GetMemberInfo().Name);
} 

此行发生错误:

NotifyOfPropertyChanged(property.GetMemberInfo().Name);

错误如下:

'System.Linq.Expressions.Expression<System.Func<TProperty>>' does not contain a definition for 'GetMemberInfo' and no extension method 'GetMemberInfo' accepting a first argument of type 'System.Linq.Expressions.Expression<System.Func<TProperty>>' could be found (are you missing a using directive or an assembly reference?

1 个答案:

答案 0 :(得分:2)

public static MemberInfo GetMemberInfo(this Expression expression)
{
    var lambda = (LambdaExpression)expression;

    MemberExpression memberExpression;
    if (lambda.Body is UnaryExpression)
    {
        var unaryExpression = (UnaryExpression)lambda.Body;
        memberExpression = (MemberExpression)unaryExpression.Operand;
    }
    else
        memberExpression = (MemberExpression)lambda.Body;

    return memberExpression.Member;
}