简体   繁体   English

如何将“out”参数传递给 lambda 表达式

[英]How to pass 'out' parameter into lambda expression

I have a method with the following signature:我有一个具有以下签名的方法:

private PropertyInfo getPropertyForDBField(string dbField, out string prettyName)

In it, I find the associated value prettyName based on the given dbField .在其中,我根据给定的dbField prettyName I then want to find all properties, if any, that have the name prettyName , so I'm trying to do the following:然后,我想查找名称为prettyName的所有属性(如果有),因此我尝试执行以下操作:

IEnumerable<PropertyInfo> matchingProperties =
    getLocalProperties().Where(prop =>
        prop.Name.Equals(prettyName)
    );

However, this gives the following error:但是,这会产生以下错误:

Cannot use ref or out parameter 'prettyName' inside an anonymous method, lambda expression, or query expression不能在匿名方法、lambda 表达式或查询表达式中使用 ref 或 out 参数“prettyName”

By the point in the method where I'm trying to use prettyName in the Where lambda parameter, prettyName is definitely initialized.在我尝试在Where lambda 参数中使用prettyName的方法中, prettyName肯定已被初始化。 I return if prettyName cannot be initialized to a valid value.如果prettyName无法初始化为有效值,我会return Is there some trick I could do here to let me use prettyName in the lambda expression?我可以在这里做一些技巧来让我在 lambda 表达式中使用prettyName吗?

Edit: I'm using .NET 3.5 if it matters.编辑:如果重要的话,我正在使用 .NET 3.5。

Just to clarify.只是为了澄清。 It's possible to use ref/out arguments from a called method in a lambda.可以在 lambda 中使用来自被调用方法的 ref/out 参数。

You can also use a ref or out if you specify type of the parameter.如果指定参数的类型,也可以使用 ref 或 out。 Which means sending prettyName as a parameter to the lambda.这意味着将 prettyName 作为参数发送给 lambda。

(prop, ref string prettyName) => prop.Name.Equals(prettyName);

Where clause takes in only one argument, which is the property element in the list. Where 子句只接受一个参数,即列表中的属性元素。 This is what prevents you from adding an argument to the lambda.这就是阻止您向 lambda 添加参数的原因。

Didn't want to leave people the false impression that you cannot use these arguments in a lambda.不想给人们留下无法在 lambda 中使用这些参数的错误印象。 You just can't use them by capture.您只是不能通过捕获来使用它们。

As the compiler error indicates, it isn't allowed to use out or ref parameters inside lambda expressions.正如编译器错误所示,不允许在 lambda 表达式中使用 out 或 ref 参数。

Why not just use a copy?为什么不直接使用副本? It's not like the lambda wants to mutate the variable anyway, so I don't see a downside.无论如何,lambda 都不想改变变量,所以我看不出有什么缺点。

string prettyNameCopy = prettyName;
var matchingProperties = getLocalProperties()
                        .Where(prop => prop.Name == prettyNameCopy);

Alternatively, you can use a local throughout (to evaluate the appropriate name etc.), and assign the out parameter prettyName just before returning from the method.或者,您可以使用局部变量(以评估适当的名称等),并在从方法返回之前分配out参数prettyName This will probably be more readable if there isn't significant branching within the method.如果方法中没有明显的分支,这可能会更具可读性。

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

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