PoshCode Logo PowerShell Code Repository

Difference between
modified post 3661 by Oisin Grehan on Mon 24th Sep 13:43 and
original post 3656 by Oisin Grehan on Sat 22nd Sep 12:54
Showold version | new version | both versions

    
11
function ConvertTo-Module {
22
<#
33
    .SYNOPSIS
44
    Quickly convert a .NET type's static methods into functions
66
    .DESCRIPTION
77
    Quickly convert a .NET type's static methods into functions.
99
    This function returns a PSModuleInfo, so you should pipe its
1010
    output to Import-Module to use the exported functions.
1212
    .PARAMETER Type
1313
    The type from which to import static methods.
1515
    .INPUTS
1616
    System.String, System.Type
1818
    .OUTPUTS
1919
    PSModuleInfo
2121
    .EXAMPLE
2222
    ConvertTo-Module System.Math | Import-Module -Verbose
2424
    .EXAMPLE
2525
    [math] | ConvertTo-Module | Import-Module -Verbose
2727
#>
2828
    [outputtype([psmoduleinfo])]
2929
    param(
3030
        [parameter(
3131
            position=0,
3232
            valuefrompipeline=$true,
3333
            mandatory=$true)]
3434
        [validatenotnull()]
3535
        [type]$Type
3636
    )
3838
    new-module {
3939
        param($type)
4141
        ($exports = $type.getmethods("static,public").Name | sort -uniq) | `
4242
            % {
4343
                $func = $_
4444
                new-item "function:script:$($_)" `
4545
                    -Value {
4646
                        # look mom! no [scriptblock]::create!
47-
                        ($type.Name -as [type])::$func.invoke($args)
47+
                        ($type.FullName -as [type])::$func.invoke($args)
4949
                    }.GetNewClosure() # capture the value of $func
5050
            }
5151
        export-modulemember -function $exports
5252
    } -name $type.Name -ArgumentList $type
5353
}

ContributeMost Recent Contributions (feed)

Contribute ... Next Page