Equivalent to C#'s “using” keyword in powershell?

前端 未结 9 1887
臣服心动
臣服心动 2020-12-04 14:13

When I use another object in the .net-Framework in C# I can save a lot of typing by using the using directive.

using FooCompany.Bar.Qux.Assembly.With.Ridicul         


        
9条回答
  •  [愿得一人]
    2020-12-04 15:03

    If you just need to create an instance of your type, you can store the name of the long namespace in a string:

    $st = "System.Text"
    $sb = New-Object "$st.StringBuilder"
    

    It's not as powerful as the using directive in C#, but at least it's very easy to use.

提交回复
热议问题