0

叹。改天,另一种 PowerShell 方法与 .NET 交互,我只是不明白。这次使用签名的 XML,但如何创建新对象是个问题。

$signedXml = New-Object system.security.cryptography.xml.signedXml -argumentList:$xml作品。但在可能的情况下,我一直在搬到[type]::New(). 而且…… $signedXml = [System.Security.Cryptography.Xml.SignedXml]::New($xml)没用。在一个脚本中。在 ISE 中工作正常,但是当作为脚本运行时,我得到

找不到类型 [System.Security.Cryptography.Xml.SignedXml]。

因此,幕后发生的事情是,使用构造函数仅适用于 ISE,而 New-Object 也适用于脚本。而且,人们如何理解将要失败的事情?我有很多其他的东西我已经搬到[type]::New()了没有问题的地方。当构造函数失败时,我唯一的选择是使用命令行开关吗?在我看来,这会导致代码的一致性和可读性降低。

4

2 回答 2

1

在我这样做之前,它在 ISE 中也对我不起作用。也许您在 ISE 中加载了一些类似的模块。

using assembly system.security
于 2019-11-08T21:21:11.737 回答
0

尝试:

Using namespace System.Security.Cryptography.Xml;

Powershell 能够调用像 C# 这样的命名空间。

下面的代码在 Powershell Core 中似乎对我很好:

Using namespace System.Security.Cryptography.Xml;

$xml = [xml]::New()
$signed = [SignedXml]::New($xml)
于 2019-11-08T22:25:30.793 回答