使用 Powershell,System.Management.Automation.Cmdlet.Invoke() 返回类型的对象'<Invoke>d__40'
而不是指定的 OutputType。
重现:
- Copy SendGreeting 示例 cmdlet到 .\ExampleCmdlet.cs
powershell -NoProfile
Add-Type -Path .\ExampleCmdlet.cs
$command = [SendGreeting.SendGreetingCommand]::new()
$command.Name = 'Person'
$invoke = $command.Invoke()
$invoke.GetType()
Expected: [string]
Actual: [<Invoke>d__40]
$PS版本表:
Name Value
---- -----
PSVersion 5.1.19041.1237
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.19041.1237
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
Get-Member -InputObject $invoke
揭示这一点来实施IE枚举器和一些玩.MoveNext()
and .Current
有时会输出预期的"Hello Person!"
result.
这是什么<Invoke>d__40
type?
Why is $command.Invoke()
不直接返回预期的字符串输出?
<Invoke>d__40
是编译器生成的类的名称:
Cmdlet.Invoke
/Cmdlet.Invoke<T>
回报IEnumerable
/IEnumerable<T>
and 已实施 using yield return这会导致编译器生成特殊的命名类(编译器可以使用<
and >
标识符中的符号,而开发人员不能)它实现IEnumerable
/IEnumerable<T>
(例如查看this反编译)。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)