【问题标题】:PowerShell Resolving .NET Tab Completion Methods and PropertiesPowerShell 解析 .NET 选项卡完成方法和属性
【发布时间】:2020-01-01 22:14:02
【问题描述】:

我可以在 PowerShell 控制台上键入此 [System.,然后使用 Tab 补全,但有没有办法解析 . 之后的所有可能值?我知道我可以使用 Ctrl+Space显示所有值,但是我可以通过编程方式返回所有值吗?

同样,我可以输入 [System.IO.Path]::,然后 Tab 补全将显示像 GetFileNameWithoutExtension 这样的属性(同样,我可以使用 Ctrl+Space 来显示值) 但为此,有没有办法以编程方式返回所有可能的 .NET 方法和属性?

【问题讨论】:

    标签: .net powershell console


    【解决方案1】:

    CRTL+空格键 与 [System.还有……

    如图……

    [System. # then CTRL+Spacebar
    
    Loading personal and system profiles took 3487ms.
     [System.
    Display all 241 possibilities? (y or n) _ 
    
    # Select 'y'
    
     [System.
    AccessViolationException               EventHandler                           OutOfMemoryException
    Action<>                               Exception                              OverflowException
    ActivationContext                      ExecutionEngineException               ParamArrayAttribute
    Activator                              FieldAccessException                   PlatformID
    AdjustmentRule                         FileStyleUriParser                     PlatformNotSupportedException...
    

    如果你的意思是,给你一个弹出列表供你选择,就像 ISE 或 VSCode 或 Visual Studio 那样,那么不,这就是 ISE / VSCode / Visual Studio 和其他 PowerShell 编辑器存在的原因,以帮助人们在继续前进之前,谁想看到可用的善良。

    至于……

    有没有办法以编程方式返回所有可能的 .NET 方法 和属性?

    这只是我在我的个人资料/我的个人模块中汇总的部分内容,用于查找您似乎也在追求的此类用例。

    ### Query Powershell Data Types
    [AppDomain]::CurrentDomain.GetAssemblies() | 
    Foreach-Object { $_.GetExportedTypes() }
    
    # Or 
    
    [psobject].Assembly.GetType(“System.Management.Automation.TypeAccelerators”)::get
    
    # Or
    
        [psobject].Assembly.GetType("System.Management.Automation.TypeAccelerators")::Get.GetEnumerator() | 
    Sort-Object -Property Key
    
    
    # Finding the properties of a .NET class
    [System.Environment].DeclaredProperties.Name
    
    
    # Looping through static properties.
    $obj = [environment]
    $obj | get-member -Static -MemberType property | 
    foreach name | 
    foreach { "$_ = $($obj::$_)" }
    
    
    <#
     Get any .NET types and their static methods from PowerShell. 
     Enumerate all that are currently loaded into your AppDomain.
    #>  
    [AppDomain]::CurrentDomain.GetAssemblies() | 
    foreach { $_.GetTypes() } | 
    foreach { $_.GetMethods() } | 
    where { $_.IsStatic } | 
    select DeclaringType, Name | 
    Out-GridView -PassThru -Title '.NET types and their static methods'
    
    # Instantiate the types using new-object and call instance methods. 
    # You can use get-member on an instance to get the methods on a type.
    
    $Object = [psobject].Assembly.GetType(“System.Management.Automation.TypeAccelerators”)::get
    $Object | Get-Member
    $Object | Get-Member -Static
    $Object.GetType()
    $Object.GetEnumerator()
    
    
    # Show Public methods
    [System.Management.Automation.ModuleIntrinsics]::GetModulePath
    
    # Show Private methods
    [System.Management.Automation.ModuleIntrinsics] | 
    Get-Member -Static
    
    ### .Net API Browsers
    <#
    https://docs.microsoft.com/en-us/dotnet/api/?view=netframework-4.8
    https://docs.microsoft.com/en-us/dotnet/framework/additional-apis/index
    http://pinvoke.net
    #>
    

    在我的个人资料/ModuleLibrary 中,我会根据需要在我的代码中直接或间接调用这些以及我在那里拥有的更多内容。

    【讨论】: