且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在Powershell中通过引用传递字符串?

更新时间:2023-11-14 13:10:40

您的第一个代码段在概念上是正确的,并且可以按预期工作-它本身不会产生 无法在此对象上找到属性值"".

Your first snippet is conceptually correct and works as intended - by itself it does not produce the "Property 'Value' cannot be found on this object" error.

由于以下行,您仅在链接到的完整脚本中看到错误:

You're seeing the error only as part of the full script you link to, because of the following line:

$btnSubmit.Add_Click({ Submit })

此行使您的 Submit 函数被调用为不带参数,这又导致 $ firstName 参数值成为$ null ,当您将其分配给 $ firstName.Value 时,这又导致上面引用的错误.

This line causes your Submit function to be called without arguments, which in turn causes the $firstName parameter value to be $null, which in turn causes the error quoted above when you assign to $firstName.Value.

相反,如您的第一个代码段中所述,对 Submit 的以下调用是正确的:

By contrast, the following invocation of Submit, as in your first snippet, is correct:

Submit ([ref] $firstName)  # Note the recommended space after 'Submit' - see below.

[ref] $ firstName 创建对调用方 $ firstName 变量的(临时)引用,该引用位于 Submit code>绑定到(本地)参数变量 $ firstName (这两个名称可能相同,但不一定,***不要具有相同的名称),其中, $ firstName然后可以使用.Value 修改调用方的 $ firstName 变量.

[ref] $firstName creates a (transient) reference to the caller's $firstName variable, which inside Submit binds to (local) parameter variable $firstName (the two may, but needn't and perhaps better not have the same name), where $firstName.Value can then be used to modify the caller's $firstName variable.

语法注释:我故意在 Submit ([ref] $ firstName)之间放置一个空格,以使内容更清楚:

Syntax note: I've intentionally placed a space between Submit and ([ref] $firstName) to make one thing clearer:

此处的(...)(括号)不会将整个参数 list 括起来,就像在 method 调用中那样,它们包含单个参数 [ref] $ firstName -的必要性,因为否则 expression 不会被这样识别.

The (...) (parentheses) here do not enclose the entire argument list, as they would in a method call, they enclose the single argument [ref] $firstName - of necessity, because that expression wouldn't be recognized as such otherwise.

函数调用以所谓的 argument模式进行解析,其语法更类似于调用控制台应用程序的语法:参数以空格分隔,并且通常仅如果它们包含特殊字符,则需要引用.

Function calls in PowerShell are parsed in so-called argument mode, whose syntax is more like that of invoking console applications: arguments are space-separated, and generally only need quoting if they contain special characters.

例如,如果您还想将字符串'foo'作为第二个位置参数传递给 Submit :

For instance, if you also wanted to pass string 'foo', as the 2nd positional parameter, to Submit:

Submit ([ref] $firstName) foo

请注意两个参数之间如何用空格分隔,以及 foo 不需要用引号引起来.

Note how the two arguments are space-separated and how foo needn't be quoted.

关于替代方法:

[ref] 的主要目的是启用具有 ref / out 参数的.NET方法调用,并且如上所述,使用 [ref] 并非易事.

[ref]'s primary purpose is to enable .NET method calls that have ref / out parameters, and, as shown above, using [ref] is nontrivial.

对于调用PowerShell函数,通常有更简单的解决方案.

For calls to PowerShell functions there are generally simpler solutions.

例如,您可以将自定义对象传递给函数,并让该函数使用要返回的值更新其属性,这自然允许多个值被归还";例如:

For instance, you can pass a custom object to your function and let the function update its properties with the values you want to return, which naturally allows multiple values to be "returned"; e.g.:

function Submit($outObj){ 
    $outObj.firstName = 'a first name'
}

# Initialize the custom object that will receive values inside
# the Submit function.
$obj = [pscustomobject] @{ firstName = $null }

# Pass the custom object to Submit.
# Since a custom object is a reference type, a *reference* to it
# is bound to the $outObj parameter variable.
Submit $obj

$obj.firstName # -> 'a first name'

或者,您可以让 Submit 构造自定义对象本身,然后简单地 output 它:

Alternatively, you can just let Submit construct the custom object itself, and simply output it:

function Submit { 
    # Construct and (implicitly) output a custom
    # object with all values of interest.
    [pscustomobject] @{ 
        firstName = 'a first name' 
    } 
}

$obj = Submit

$obj.firstName # -> 'a first name'