如何调用新对象的构造函数接受一个数组参数? [英] How do I call New-Object for a constructor which takes a single array parameter?

查看:269
本文介绍了如何调用新对象的构造函数接受一个数组参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在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 constructor new X509Certificate2(byte[] byteArray). The problem is when I do this with a byte array from powershell, I get

New-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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆