gpt4 book ai didi

c# - 尝试创建一个自定义 cmdlet,它将 [ref] 作为参数

转载 作者:太空宇宙 更新时间:2023-11-03 14:06:52 25 4
gpt4 key购买 nike

//powershell code

$quiotent = divRemainderQuiotent ($a) ($b) ([ref] $remainder) # I need to pass this $remainder as reference

为此我需要将它作为 PSReference 传递

//csharp code 

private PSReference remainder= null;
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1044:PropertiesShouldNotBeWriteOnly"),
Parameter(
Position = 2,
Mandatory = true,
ValueFromPipeline = true)]
[ValidateNotNullOrEmpty]
public PSReference Remainder // if I don’t use this and use a value type and later change it to ref (inside C#) the result won’t bubble up, which I think is bad.
{
set { remainder = value; }
}

但现在问题变成了,将这个参数作为 ref 整数的函数。

  protected override void ProcessRecord()
{
//main method, this is executed when cmdlet is run
int r = remainder ; //this is the problem I cannot convert PSReference to other object type (integer here).
int q = _csharpDivideRemainderQuiotent(a,b, ref r); // if I change this function to take PSReference type, I will have to make changes down the line for every function.
WriteObject(q);
} // method ProcessRecord

最佳答案

我以前没有使用过 PSReference,但你不能这样做吗:

protected override void ProcessRecord() 
{
//main method, this is executed when cmdlet is run
int r = Convert.ToInt32(this.Remainder.Value);
int q = _csharpDivideRemainderQuiotent(a,b, ref r);
this.Remainder.Value = r;
WriteObject(q);
}

关于c# - 尝试创建一个自定义 cmdlet,它将 [ref] 作为参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9099756/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com