Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net PowerShell类型名称求值_.net_Powershell_Scripting_Powershell Ise_Powershell 4.0 - Fatal编程技术网

.net PowerShell类型名称求值

.net PowerShell类型名称求值,.net,powershell,scripting,powershell-ise,powershell-4.0,.net,Powershell,Scripting,Powershell Ise,Powershell 4.0,不幸的是,Powershell ISE无法为.NET类构造函数显示intellisense。 我找到了一个脚本,可以获取构造函数列表: [System.IO.StreamWriter].GetConstructors() | ForEach-Object { ($_.GetParameters() | ForEach-Object { ‘{0} {1}’ -f $_.Name, $_.ParameterType.FullName

不幸的是,Powershell ISE无法为.NET类构造函数显示intellisense。 我找到了一个脚本,可以获取构造函数列表:

[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

您需要从$Class字符串参数创建类型对象:

$script = {
Param(
    [Parameter(Mandatory=$True)]
    [string]$class
)

([Type]$Class).GetConstructors() |
 ForEach-Object {
    ($_.GetParameters() |
        ForEach-Object {
            ‘{0} {1}’ -f $_.Name, $_.ParameterType.FullName 
        }) -join ‘,’
 }
 }

 &$script 'system.io.streamwriter'

stream System.IO.Stream
stream System.IO.Stream,encoding System.Text.Encoding
stream System.IO.Stream,encoding System.Text.Encoding,bufferSize System.Int32
stream System.IO.Stream,encoding System.Text.Encoding,bufferSize System.Int32,leaveOpen System.Boolean
path System.String
path System.String,append System.Boolean
path System.String,append System.Boolean,encoding System.Text.Encoding
path System.String,append System.Boolean,encoding System.Text.Encoding,bufferSize System.Int32

很抱歉,我忘了添加以下内容:script.ps1 System.IO.StreamWriter。在$class变量中,我将有一个strign,表示类类型的全名。应该可以这样做。我用示例用法更新了脚本。是的,它可以工作,谢谢!。这里是我刚刚找到的另一个方法:[System.Type]::GetType($class).GetConstructors()|…您可能还可以将
[string]$class
替换为
[type]$class