本文介绍了.Net Lambda表达式-此参数来自何处?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是lambda新手,因此,如果我在说明中缺少重要信息,请告诉我。我将使示例尽可能简单。

I'm a lambda newbie, so if I'm missing vital information in my description please tell me. I'll keep the example as simple as possible.

我正在查看别人的代码,他们的一个类继承自另一个。首先是派生类,以及我难以理解的lambda表达式:

I'm going over someone else's code and they have one class inheriting from another. Here's the derived class first, along with the lambda expression I'm having trouble understanding:

    class SampleViewModel : ViewModelBase
{
    private ICustomerStorage storage = ModelFactory<ICustomerStorage>.Create();

    public ICustomer CurrentCustomer
    {
        get { return (ICustomer)GetValue(CurrentCustomerProperty); }
        set { SetValue(CurrentCustomerProperty, value); }
    }

    private int quantitySaved;
    public int QuantitySaved
    {
        get { return quantitySaved; }
        set
        {
            if (quantitySaved != value)
            {
                quantitySaved = value;
                NotifyPropertyChanged(p => QuantitySaved); //where does 'p' come from?
            }
        }
    }

    public static readonly DependencyProperty CurrentCustomerProperty;

    static SampleViewModel()
    {
        CurrentCustomerProperty = DependencyProperty.Register("CurrentCustomer", typeof(ICustomer),
            typeof(SampleViewModel), new UIPropertyMetadata(ModelFactory<ICustomer>.Create()));
    }
//more method definitions follow..

请注意对上面的 NotifyPropertyChanged(p => QuantitySaved)位。我不知道 p的来源。

Note the call to NotifyPropertyChanged(p => QuantitySaved) bit above. I don't understand where the "p" is coming from.

这是基类:

  public abstract class ViewModelBase : DependencyObject, INotifyPropertyChanged, IXtremeMvvmViewModel
    {
        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void NotifyPropertyChanged<T>(Expression<Func<ViewModelBase, T>> property)
        {
            MvvmHelper.NotifyPropertyChanged(property, PropertyChanged);
        }
    }

里面有很多与问题无关的东西我确定,但是我想在包容性方面犯错。

There's a lot in there that's not germane to the question I'm sure, but I wanted to err on the side of inclusiveness.

问题是,我不知道'p'参数的来源,以及编译器如何知道(显然?)凭空填充ViewModelBase的类型值?

The problem is, I don't understand where the 'p' parameter is coming from, and how the compiler knows to (evidently?) fill in a type value of ViewModelBase from thin air?

为了娱乐,我将代码从 p更改为 this ,因为SampleViewModel继承自ViewModelBase,但是遇到了一系列编译器错误,其中第一个错误是无效的表达式术语'=>',这使我有些困惑

For fun I changed the code from 'p' to 'this', since SampleViewModel inherits from ViewModelBase, but I was met with a series of compiler errors, the first one of which statedInvalid expression term '=>' This confused me a bit since I thought that would work.

有人可以解释这里发生了什么吗?

Can anyone explain what's happening here?

推荐答案

lambda p => QuantitySaved 是类型 Expression< Func< ViewModelBase,int>> 的表达式。由于方法 NotifyPropertyChanged 在查找< ViewModelBase,T> 的表达式,因此适合。

The lambda p => QuantitySaved is an expression of type Expression<Func<ViewModelBase, int>>. Since the method NotifyPropertyChanged is looking for an expression of <ViewModelBase, T>, it fits.

因此,编译器能够推断 p ViewModelBase p 并不是从任何地方来的,基本上是在这里声明的。这是lambda的参数当有人使用您的方法的属性参数时,它将被填写。例如,如果将lambda放入名为 lambda 的单独变量中,则可以使用 lambda(this)进行调用并返回 QuantitySaved 值。

So the compiler is able to infer that p is a ViewModelBase. p didn't "come from" anywhere, it's basically being declared right here. It's a parameter for the lambda. It's going to be filled in when someone uses the property parameter of your method. For example, if you put your lambda into a separate variable called lambda, you could call it with lambda(this) and it would return the QuantitySaved value.

不能使用的原因lambda中的是因为它需要一个参数名称,而 this 不是有效的名称。关键是您可以在 ViewModelBase 的任何实例上调用它,而不仅仅是创建lambda的实例。

The reason you can't use this in the lambda is because it's expecting a parameter name, and this isn't a valid name. The point is that you could call it on any instance of ViewModelBase, not just the one that created the lambda.

这篇关于.Net Lambda表达式-此参数来自何处?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 15:11