我到处都在寻找它,我已经为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?

最佳答案

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

09-06 00:30