PoshCode Logo PowerShell Code Repository

Import-Methods by Joel Bennett 4 years ago
View followups from Joel Bennett | embed code: <script type="text/javascript" src="http://PoshCode.org/embed/162"></script>download | new post

Add functions to the scope for each static method of a type. Originally from Oisin Grehan
GREAT example: . Import-Methods Math

  1. param([type]$type = $(throw "need a type!"))
  2.  
  3. $type | Get-Member -static | Where {$_.membertype -eq "method" } |
  4. % {
  5.    $func = "function:$($_.name)"
  6.    if (test-path $func) { remove-item $func }
  7.    new-item $func -value "[$($type.fullname)].InvokeMember('$($_.name)','Public,Static,InvokeMethod,DeclaredOnly', `$null, `$null, `$args[0])"
  8. }

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me