//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
答案 0 :(得分:1)
之前我没有使用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);
}