将多个参数传递给类方法的PowerShell失败 [英] PowerShell Passing Multiple Parameters to a Class Method Fails

查看:133
本文介绍了将多个参数传递给类方法的PowerShell失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个奇怪的PowerShell问题.我试图将多个参数传递给类方法,但是失败.我能够将多个参数传递给全局函数,并且可以正常工作.尝试将可变数量的参数传递给类方法会失败!

这是我的代码:

class TestParams {
    [int] $dummyVar

    TestParams() {
        $this.dummyVar = 0
    }

    [void]myMethod() {
        for ($index = 0; $index -lt $args.Count; $index++) {
            Write-Host $args[$index]
        }
    }
}

function testFunc() {
    for ($index = 0; $index -lt $args.Count; $index++) {
        Write-Host $args[$index]
    }
}

testFunc '1' '2' '3' # works

$myTestObj = New-Object TestParams
$myTestObj.myMethod "A" "B" "C" # fails

从运行我的代码可以看到,它给出了诸如以下的错误消息:

At C:\*****\testParams.ps1:25 char:21
+ $myTestObj.myMethod "A" "B" "C"
+                     ~~~
Unexpected token '"A"' in expression or statement.

我不知道是什么原因导致此错误!你们能帮我调试一下吗?

解决方案

PowerShell Mathias R. Jessen的有用答案显示了如何通过 array 参数,模拟仅适用于功能$Args功能.


[1]由于PowerShell Core 6.2.0-rc.1中存在一个错误,因此$Args可能会意外地在方法中被引用-尽管尚未初始化-但总会得出空数组-请参见此GitHub问题. >

I have a weird PowerShell problem. I am trying to pass into a class method multiple parameters, but it fails. I was able to pass in multiple parameters to a global function instead and that worked. Trying to pass in a variable amount of parameters to a class method instead fails!

Here is my code:

class TestParams {
    [int] $dummyVar

    TestParams() {
        $this.dummyVar = 0
    }

    [void]myMethod() {
        for ($index = 0; $index -lt $args.Count; $index++) {
            Write-Host $args[$index]
        }
    }
}

function testFunc() {
    for ($index = 0; $index -lt $args.Count; $index++) {
        Write-Host $args[$index]
    }
}

testFunc '1' '2' '3' # works

$myTestObj = New-Object TestParams
$myTestObj.myMethod "A" "B" "C" # fails

As you can see from running my code it gives error messages such as:

At C:\*****\testParams.ps1:25 char:21
+ $myTestObj.myMethod "A" "B" "C"
+                     ~~~
Unexpected token '"A"' in expression or statement.

I do not know what is causing this error! Can you guys help me debug this?

解决方案

PowerShell custom classes (PSv5+) work more like C# code, not like PowerShell functions and scripts:

  • Method / constructor declarations as well as calls must use method syntax, i.e., (...) around a list of ,-separated arguments rather than command syntax (space-separated arguments without enclosing (...)); e.g., if .MyMethod() had 3 distinct parameters:

    • $obj.MyMethod('A', 'B', 'C') instead of $obj.MyMethod 'A' 'B' 'C'
  • Any arguments you pass must bind to formally declared parameters - there is no support for accessing arbitrary arguments via automatic variable$Args.[1]

  • There is no implicit output behavior: Unless methods don't declare a return type or use [void], they must use return to return a value.

Mathias R. Jessen's helpful answer shows how to implement your method with an open-ended number of arguments via an array parameter, emulating the $Args functionality available only to functions.


[1] Due to a bug as of PowerShell Core 6.2.0-rc.1, $Args can unexpectedly be referenced in a method - despite not having been initialized - but always evaluates to an empty array - see this GitHub issue.

这篇关于将多个参数传递给类方法的PowerShell失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆