PowerShell问题:Indoke Method&quot不能施放类型的对象。键入System.String []’
我目前正在尝试在PowerShell中运行.NET应用程序。我正在使用System.Reflection.sembly加载字节以记忆C#编写的汇编。
.NET应用程序具有一种方法,main(System.String [] Args)
,它将列出传递的值作为函数的参数。非常简单的程序。
这是PowerShell代码:
[String] $assemblyPath = "C:\Users\HakkYahud\Desktop\HelloWorld.exe"
[String[]]$parameter_main = @( "test" )
[Byte[]]$assemblyByte = [System.IO.File]::ReadAllBytes($assemblyPath)
$assembly = [System.Reflection.Assembly]::Load($assemblyByte)
$entryPoint = $assembly.EntryPoint
$parameter_invoke = @( $parameter_main )
Write-Host $parameter_main.GetType()
Write-Host $parameter_invoke.GetType()
Write-Host $entryPoint
Write-Host @( "test", "test2" ).GetType()
$entryPoint.Invoke($null, $parameter_invoke)
我知道该方法Indoke
采用2个参数Object OBJ
object []参数。
运行代码后,函数调用中会发生错误,它说“它不能铸造系统。string to type system.string []”
变量$ parameter_main
已经是system 。
此外,当我放置$ parameter_main = @(“ test”,“ test2”)
时,我会收到另一个错误,说“ 错误的参数
”已经遇到这个问题了吗?
I'm currently trying to run a .NET application in PowerShell. I'm using System.Reflection.Assembly to load the bytes in memory of an assembly written in C#.
The .NET application has one method, Main(System.String[] args)
, and it will list the values passed as argument of the function. Very simple program.
Here's the PowerShell code :
[String] $assemblyPath = "C:\Users\HakkYahud\Desktop\HelloWorld.exe"
[String[]]$parameter_main = @( "test" )
[Byte[]]$assemblyByte = [System.IO.File]::ReadAllBytes($assemblyPath)
$assembly = [System.Reflection.Assembly]::Load($assemblyByte)
$entryPoint = $assembly.EntryPoint
$parameter_invoke = @( $parameter_main )
Write-Host $parameter_main.GetType()
Write-Host $parameter_invoke.GetType()
Write-Host $entryPoint
Write-Host @( "test", "test2" ).GetType()
$entryPoint.Invoke($null, $parameter_invoke)
I know that method Invoke
takes 2 parameters Object obj
and Object[] parameters
.
Once running the code, an error occurs in the function Invoke, it is saying that "it cannot cast System.String to the type System.String[]"
The variable $parameter_main
is already System.String[]
, why does the compiler need to cast from System.String
to System.String[]
?
Moreover, when I'm putting $parameter_main = @("test", "test2")
, I'm receiving another error saying "Wrong number of arguments"
Has anyone already encountered this issue?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Powershell的管道处理器喜欢枚举输出。这意味着,当您使用阵列子表达
@(...)
之类的时:... PowerShell评估
$ ninnarray
,看到它是一个数组,请枚举所有项目在数组中,以及在$ upourarray
中存储的结果数组值,因此变成了一个平坦的数组,其中包含与$ $ ninnarray
完全相同的项目。为了防止此数组 - 浅板,有两个选项:
写入输出-Noenumerate
以表示PowerShell应该 试图枚举内部数组中的项目:$ parameter_invoke = @(书面输出$ parameter_main -noEnumerate)
$ parameter_invoke = @(,$ parameter_main)
PowerShell's pipeline processor loves enumerating output. This means that when you use the array subexpression
@(...)
like this:... PowerShell evaluates
$innerArray
, sees that it's an array, enumerates all the items in the array, and the resulting array value stored in$outerArray
thus becomes a flat array containing the exact same items as$innerArray
.To prevent this array-flattening, there are two options:
Write-Output -NoEnumerate
to signal that PowerShell should not attempt to enumerate the items from inner array:$parameter_invoke = @( Write-Output $parameter_main -NoEnumerate )
$parameter_invoke = @( ,$parameter_main )