不幸的是,Powershell ISE 无法为 .NET 类构造函数显示智能感知。我找到了一个可以获取构造函数列表的脚本:
[System.IO.StreamWriter].GetConstructors() |
ForEach-Object {
($_.GetParameters() |
ForEach-Object {
‘{0} {1}’ -f $_.Name, $_.ParameterType.FullName
}) -join ‘,’
}
它完美地工作
有没有办法使用变量而不是固定的类名?有人这样想:
Param(
[Parameter(Mandatory=$True)]
[string]$class
)
[$class].GetConstructors() |
ForEach-Object {
($_.GetParameters() |
ForEach-Object {
‘{0} {1}’ -f $_.Name, $_.ParameterType.FullName
}) -join ‘,’
}
我将以这种方式调用脚本:
Script.ps1 System.IO.StreamWriter
现在它返回以下错误:
在 D:\LAB\POWERSHELL\CMD-LETS\GetConstructor.ps1:6 char:2 + [$class].GetConstructors() | + ~ '[' 后缺少类型名称。在 D:\LAB\POWERSHELL\CMD-LETS\GetConstructor.ps1:6 char:26 + [$class].GetConstructors() | + ~ 在 '(' 之后应该有一个表达式。+ CategoryInfo : ParserError: (:) [], ParseException + FullyQualifiedErrorId : MissingTypename