by oising
13. August 2007 20:27
Someone on the PowerShell NG asked recently if there was some kind of equivalent to c#'s "using namespace;" or VB.NET's "Imports namespace" statements. Well, the short answer to that is no, not really. However, you can simulate it by creating functions for each static method on the class:
-
param([type]$type = $
(throw
"need a type!"))
-
-
$type | gm -static | ? {$_.membertype -eq "method" } | % {
-
$func = "function:$($_.name)"
-
if (test-path $func) { remove-item $func }
-
$flags = 'Public,Static,InvokeMethod,DeclaredOnly'
-
new-item $func -value "[$($type.fullname)].InvokeMember('$($_.name)', ${flags}, `$null, `$null, `$args[0])"
-
}
Save this as "import.ps1" for example and use like so:
PS > . .\import.ps1 ([Math])
...
PS > Sin 1
0.841470984807897
Methods that take multiple args must have them passed as a single array:
PS > Max @(1,2)
2
Have fun!
Update: there are some interesting extensions appearing based around my initial post on microsoft.public.windows.powershell.