简体   繁体   English

Lambda函数使用未知参数

[英]Lambda Function Using Unknown Parameter

I see PRISM declaring the following constructor, and I don't understand what's that "o" being used with the lambda function that serves as the second parameter when the base constructor is called: 我看到PRISM声明了以下构造函数,我不明白当调用基本构造函数时,作为第二个参数的lambda函数使用的“o”是什么:

public DelegateCommand(Action<T> executeMethod)
    : this(executeMethod, (o)=>true)
{            
}

I'd appreciate an explanation. 我很感激解释。

The constructor which declaration you posted calls another constructor, so to explain it, we should first look at the other constructor's signature: 你发布的声明的构造函数调用另一个构造函数,所以为了解释它,我们首先应该看看其他构造函数的签名:

public DelegateCommand(Action<T> executeMethod, Func<T, bool> canExecuteMethod)

So the second parameter is a Func<T, bool> . 所以第二个参数是Func<T, bool> That means it is a function that takes a parameter of type T and returns a boolean. 这意味着它是一个函数,它接受类型为T的参数并返回一个布尔值。

Now if you look at the lambda that is used: 现在,如果你看一下使用的lambda:

(o) => true

Lambdas in general have the syntax (parameter-list) => lambda-body , so in this case, the single parameter of the lambda is a variable o (which type is inferred to be T ) and the function returns a constant result true . Lambda通常具有语法(parameter-list) => lambda-body ,因此在这种情况下,lambda的单个参数是变量o (该类型被推断为T ),并且函数返回常量结果true

The purpose of this is to basically make a command that is always executable. 这样做的目的是基本上创建一个始终可执行的命令。

Of course that lambda could look a lot more complicated, so when using the DelegateCommand, you are likely to use more complex and non-constant expressions. 当然,lambda看起来要复杂得多,所以在使用DelegateCommand时,你可能会使用更复杂和非常量的表达式。 For example: 例如:

 new DelegateCommand(DoSomething, o => o.SomeProperty >= 0 && o.SomeProperty < 10 && o.SomeBoolProperty)

It calls this constructor: 它调用这个构造函数:

DelegateCommand<T>(Action<T>, Func<T, Boolean>)

Passing a lambda that always returns true as the second parameter 传递总是返回true的lambda作为第二个参数

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

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