简体   繁体   English

我如何在Powershell的参数中像C#那样声明出来

[英]how i can declare out in parameter in powershell like c#

I have c# code and i want to find alternative of this code in PowerShell . 我有c#代码,我想在PowerShell中找到此代码的替代方法。 I found something like [ref]$parameter but it's doesn't work . 我发现了[ref]$parameter之类的东西,但是它不起作用。 My code is : 我的代码是:

private static bool testfunction(string param1, out string param 2)
{
    param1 = "";
    param2 += "Hello";
    return true;
}

Please give me alternative code in PowerShell. 请在PowerShell中给我其他代码。

i try this : 我尝试这个:

class tst 
{

    static test([ref]$param)
    {
        $param.Value  = "world "

    }
}

$test = "ddd"
$test

[tst]::test($test)
$test

this is doesn't work. 这是行不通的。

function testfunction {
   param (
       [string]
       $param1,
       [ref]
       $param2
   )

   $param2.value= "World"
   return $true
}

PS C:\> $hello = "Hello"

PS C:\> testfunction "someString" ([ref]$hello)
True

PS C:\> $hello
World

Powershell supports ref parameters. Powershell支持引用参数。 Be sure to call the ref parameter in brackets (eg ([ref] $parameter ). Be also be aware to only declare [ref] as type in the param block. Further details: 请确保在方括号中调用ref参数(例如([ref] $parameter )。还要注意,仅将[ref]声明为param块中的类型)。

stackoverflow 堆栈溢出

ss64 ss64

hope that helps 希望能有所帮助

UPDATE 更新

You've to call your test method with the ref keyword -> use [tst]::test([ref]$test) instead of `[tst]::test($test) 您必须使用ref关键字调用测试方法->使用[tst]::test([ref]$test)而不是`[tst] :: test($ test)

PS C:\> $test = "ddd"

PS C:\> $test
ddd

PS C:\> [tst]::test([ref]$test)

PS C:\> $test
world 

use [ref] when you use class : 使用class时使用[ref]

class tst 
{

    static test([ref]$param)
    {
        $param.Value  = "world "

    }
}

$test = "ddd"
$test

[tst]::test([ref]$test)
$test

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

相关问题 如何在 C# 中创建一个充当开关的 Powershell Cmdlet 参数? - How can I create a Powershell Cmdlet parameter in C# that behaves as a switch? 如何在C#中声明不可写的数组? - How can I declare a non-writable array in C#? 我怎么不声明什么类型的C#类 - How can I not declare what type of a class C# 如何在C#编写的Android中声明和设置Cursor - How can I declare and set Cursor in Android written by C# 我如何在C#中声明十六进制变量 - How can I declare hex vars in c# 在C#中,我可以发送一个out参数在别处使用 - In C#, can I send an out parameter to be used elsewhere 如何将C#类似字符串的变量传递给sbyte *参数? - How can I pass a C# String-like variable to a sbyte* parameter? 如何使用 C# 从类似 QueryString 的字符串中提取值 - How can I pull a value out of a QueryString-like string with C# 我可以在C#中使用未知类型的参数声明方法来创建接口或抽象类吗? - Can I make interfaces or abstract classes in C# that declare methods with a parameter of unknown type? 如何在C#中声明嵌入式结构(如delphi中的嵌入式记录)? - How do I declare a embedded struct in C# like a embedded record in delphi?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM