powershell - 从 Powershell 5 移植到 Powershell 7 的函数失败,并显示“对象引用未设置为对象的实例”
问题描述
以下代码在 powershell 5.1 中按预期工作:
function Get-Say([String] $Message, [String] $Voice = "Microsoft Irina Desktop") {
Add-Type -AssemblyName System.speech
$speak = New-Object System.Speech.Synthesis.SpeechSynthesizer
$speak.SelectVoice($Voice)
$speak.Speak($Message)
}
但是,在 powershell 7.1.0(预览版 5)中,执行失败并显示“对象引用未设置为对象的实例”,我实际上迷路了。谷歌搜索并没有多大帮助,因为看起来这个错误可能会在非常不同的上下文中遇到。所以,PowerShell 专家,问你一个问题,我该如何解决这个问题。
我试图在 body ( object reference not set to an instance of an object
) 中声明参数,希望可能存在一些语义差异,但这并没有帮助。
解决方案
这是一个已知问题,因为某些所需的功能在构建 PS v7 的 .NET Core 中不存在。看起来像提出了一个错误,但它在没有修复的情况下被关闭:
System.Speech.Synthesis.SpeechSynthesizer Speak 方法抛出未设置为对象实例的对象引用。”在 PowerShell 6.1.2 中
推荐阅读
- groovy - 如何使用 Ant Build 脚本打包 Groovy 类或 Groovy 项目?
- javascript - 在我的 fish / cygwin shell 上使用 Node.js
- c++ - 在 c++ 中给出意外输出的简单代码
- java - Java中的值匹配和降维
- android - 旋转视频 - 无法为输出文件 #0 写入标头(编解码器参数不正确?)
- node.js - Socket.io 在 https://domain 上建立连接,但在 https://domain/route 上没有建立任何连接
- javascript - 方法表达式不是函数类型(等于状态属性/值) - React JS
- docker - Confluent 平台和 java.nio.file.DirectoryNotEmptyException
- r - 将 Leaflet 的图标链接到 Shiny 中的情节线图
- java - 替换 org.hibernate.util.PropertiesHelper 的内容