# Monday, August 13, 2007

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:

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

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.

 



posted on Monday, August 13, 2007 4:27:05 PM (Eastern Daylight Time, UTC-04:00)  #    Comments [2] Trackback
Related posts:
PowerShell Script Provider
PowerShell ISE Hacking: Change default save encoding to ASCII
PowerShell 2.0 – PSCX Labs: Invoke-Reflector
PowerShell 2.0 – Developer Essentials #1 – Initializing a Runspace with a Module
SharePoint Resources & Localization – What, Where and Why?
PowerShell 2.0 – Partial Application of Functions and Cmdlets

Referred by:
static (search.live.com) [Referral]
powershell (search.live.com) [Referral]
".net class" powershell (www.kumo.com) [Referral]
powershell + static methods + net (www.kumo.com) [Referral]
import static methods functions powershell site:nivot.org (www.google.com) [Referral]
http://poshcode.org/969 [Referral]
powershell .net calling static methods (search.yahoo.com) [Referral]
http://blog.usepowershell.com/2009/03/exploring-the-net-fram... [Referral]
powershell array .net function (www.kumo.com) [Referral]
http://beta.poshcode.org/code.xhtml [Referral]
http://blog.usepowershell.com/ [Referral]
type accelerator (poshcode.org) [Referral]
.net static methods (search.live.com) [Referral]
.net static methods (search.live.com) [Referral]
http://beta.poshcode.org/code/969.xhtml [Referral]
PowerShell calling .NET methods (search.live.com) [Referral]
using .NET static method in powershell (www.bing.com) [Referral]
powershell call static methods (www.bing.com) [Referral]
powershell calling static method (www.bing.com) [Referral]
powershell .net static (www.bing.com) [Referral]
calling .net methods in powershell (www.bing.com) [Referral]
powershell static .net functions (www.bing.com) [Referral]
call static methods from powershell (www.bing.com) [Referral]
calling static method powershell (www.bing.com) [Referral]
http://blog.usepowershell.com/page/2/ [Referral]
powershell (www.bing.com) [Referral]
invokemember ps1 array (www.bing.com) [Referral]
call static function powershell (www.bing.com) [Referral]
calling static .net functions in powershell (www.bing.com) [Referral]
static method .net (search.yahoo.com) [Referral]
powershell static methods (www.bing.com) [Referral]
.net static method powershell (www.bing.com) [Referral]
powershell static function (www.bing.com) [Referral]
invokemember static function c# (www.bing.com) [Referral]
powershell calling .net methods (www.bing.com) [Referral]
powershell class function (www.bing.com) [Referral]
test-path equivalent in C# (www.bing.com) [Referral]
http://blog.usepowershell.com/category/net-framework/ [Referral]
powershell import .net class (www.bing.com) [Referral]
How to use static .net classes in powershell (www.bing.com) [Referral]
powershell .net methods (www.bing.com) [Referral]
wcf static functions (www.bing.com) [Referral]
.Net powershell (www.bing.com) [Referral]
calling .NET static methods from +COM (www.bing.com) [Referral]
powershell call .NET functions (www.bing.com) [Referral]
import .NET Powershell (www.bing.com) [Referral]
calling .NET methods from powershell (www.bing.com) [Referral]
call .net functions from powershell (www.bing.com) [Referral]
removing static module .net (www.bing.com) [Referral]
powershell .NET flags (www.bing.com) [Referral]
powershell max function (www.bing.com) [Referral]
static method + invokemember (www.bing.com) [Referral]
powershell import namespace (www.bing.com) [Referral]
http://poshcode.org/?list=10&page=49 [Referral]
powershell calling static class methods (www.bing.com) [Referral]
static functions powershell (www.bing.com) [Referral]
calling internal .NET static methods from powershell (www.bing.com) [Referral]
powershell max function (www.bing.com) [Referral]
.net fuction compute (www.bing.com) [Referral]
powershell list .net class methods (www.bing.com) [Referral]
Powershell + calling function (www.bing.com) [Referral]
http://www.baidu.com/s?bs=powershell+.net+object+pipline&f=8... [Referral]
powershell and calling .net classes (www.bing.com) [Referral]
c# func static methods (search.yahoo.com) [Referral]
powershell static functions (www.bing.com) [Referral]
http://technorati.com/faves?sub=addfavbtn&add=http://www.niv... [Referral]
powershell .net function (www.bing.com) [Referral]
powershell import-ps1 (www.bing.com) [Referral]
powershell static .net function (www.bing.com) [Referral]
calling c# static methods in powershell (www.bing.com) [Referral]
static c# functions from powershell (www.bing.com) [Referral]
powershell .net static (www.bing.com) [Referral]
asp.net call powershell (www.bing.com) [Referral]
call .NET function from powershell (www.bing.com) [Referral]
powershell namespace methods (www.bing.com) [Referral]
powershell namespace static .net (www.bing.com) [Referral]
powershell functions flags (www.bing.com) [Referral]
static functions in powershell (www.bing.com) [Referral]
Tuesday, August 14, 2007 10:56:01 AM (Eastern Daylight Time, UTC-04:00)
You can optimize by taking out the where statement, and use the '-membertype' paramter in get-member:

....
$type | gm -static -MemberType method | %{
...
Tuesday, August 14, 2007 2:23:34 PM (Eastern Daylight Time, UTC-04:00)
thanks James, good call.

- Oisin
Oisin
Comments are closed.