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

前端 未结 9 1956
臣服心动
臣服心动 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 14:54

    this is just a joke, joke...

    $fullnames = New-Object ( [System.Collections.Generic.List``1].MakeGenericType( [String]) );
    
    function using ( $name ) { 
    foreach ( $type in [Reflection.Assembly]::LoadWithPartialName($name).GetTypes() )
        {
            $fullnames.Add($type.fullname);
        }
    }
    
    function new ( $name ) {
        $fullname = $fullnames -like "*.$name";
        return , (New-Object $fullname[0]);
    }
    
    using System.Windows.Forms
    using FooCompany.Bar.Qux.Assembly.With.Ridiculous.Long.Namespace.I.Really.Mean.It
    $a = new button
    $b = new Thingamabob
    

提交回复
热议问题