[英]Powershell: How to pass reference of Array from Powershell to C# Function
我被困從Powershell傳遞Array的引用到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
您不能像這樣加載dll文件。 您必須這樣做:
[Reflection.Assembly]::LoadFile(".\SAMPLEAPI.dll")
然后,您可以使用所有方法:
[SAMPLEAPI.Sample]::new()
希望能幫助到你
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.