PowerShell - Set Alias for Loaded Assembly

前端 未结 4 895
傲寒
傲寒 2021-02-09 00:25

I use this code to load a .Net assembly to PowerShell:

[System.Reflection.Assembly]::Load(\"System.Windows.Forms, Version=2.0.0.0, Culture=neutral, Publ         


        
4条回答
  •  不要未来只要你来
    2021-02-09 00:44

    While you can't create some sort of namespace alias per se, you can use the following trick (taken from Lee Holmes' PowerShell Cookbook):

    $namespace = "System.Windows.Forms.{0}"
    $form = New-Object ($namespace -f "Form")
    

    But that only will work with New-Object since that takes a string for the class name. You can't use that syntax with a type name in square brackets.

    What you can do, however, is leave out the System part which is implied:

    [Windows.Forms.MessageBox]::Show("Hello World!")
    

    Makes it slightly shorter.

提交回复
热议问题