【发布时间】:2017-12-23 13:40:52
【问题描述】:
我坚持将 Array 的引用从 Powershell 传递到 C# 函数,该函数将 Out 参数作为数组。
我的 C# 项目是这样的
namespace SAMPLEAPI
{
public class TestFunction
{
public int a;
public String b;
}
public class Sample
{
public int Test(out TestFunction[] tests)
{
tests = new TestFunction[2];
tests[0].a = 1;
tests[1].a = 2;
tests[0].b = "dummy1";
tests[1].b = "dummy2";
return 1;
}
}
}
我已经将上面的项目编译成SAMPLEAPI.dll,并尝试在Powershell中调用这个函数
Powershell 脚本:
Import-Module .\SAMPLEAPI.dll
$testArray = [SAMPLEAPI.TestFunction[]]
$test = [SAMPLEAPI.Sample]::new()
$test.Test([ref] $testArray)
我收到以下异常
Exception calling "Test" with "1" argument(s): "Cannot convert the
"SAMPLEAPI.TestFunction[]" value of type
"System.RuntimeType" to type "SAMPLE.TestFunction[]"."
At line:1 char:1
+ $test.Test([ref] $a)
+ ~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : PSInvalidCastException
【问题讨论】:
-
$testArray = $null
标签: c# arrays powershell pass-by-reference