如何调用新对象的构造函数接受一个数组参数? [英] How do I call New-Object for a constructor which takes a single array parameter?
问题描述
在PowerShell中,我想用新对象
来调用一个参数的构造函数的.Net 新X509Certificate2(字节[]的字节数组)
。问题是,我做到这一点从PowerShell的字节数组时,我得到
新的对象:无法找到X509Certificate2过载和参数计数:516
块引用>解决方案此方法使用
新对象
应该工作:$证书=新的对象System.Security.Cryptography.X509Certificates.X509Certificate`
-ArgumentList @(,$字节)诀窍是,PowerShell是期待的一个数组构造参数。当只有一个参数,它是一个数组,它可以混淆PowerShell中的重载解析算法。在code。通过把字节数组与只是一个元素的数组上述帮助它。
In PowerShell, I want to use
New-Object
to call a single-argument .Net constructornew X509Certificate2(byte[] byteArray)
. The problem is when I do this with a byte array from powershell, I getNew-Object : Cannot find an overload for "X509Certificate2" and the argument count: "516".
解决方案This approach to using
new-object
should work:$cert = new-object System.Security.Cryptography.X509Certificates.X509Certificate ` -ArgumentList @(,$bytes)
The trick is that PowerShell is expecting an array of constructor arguments. When there is only one argument and it is an array, it can confuse PowerShell's overload resolution algorithm. The code above helps it out by putting the byte array in an array with just that one element.
这篇关于如何调用新对象的构造函数接受一个数组参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!