Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Using namespace directive?

296 views
Skip to first unread message

Duncan Smith

unread,
Aug 12, 2007, 3:04:36 PM8/12/07
to
Is there any equivalent of a 'using namespace' directive in
Powershell? For example, it sometimes gets a bit verbose to
explicitly qualify static functions with the type-name:

math::sin((45*([math]::pi/180)))

and was wondering whether this could be cut down with something like

using namespace system.math

sin((45*(pi/180)))

I suppose functions could be added to the $profile for sin() and pi()
which would call [math]::sin() and [math]::pi, but that's a lot of
functions to be writing?

Regards,

Duncan

Kiron

unread,
Aug 12, 2007, 6:20:10 PM8/12/07
to
Dot sourcing a function library in your script or profile, would achieve
what you are looking for.

Here are a couple of ideas for calling static members indirectly:

function _m {Invoke-Expression "[math]::$([scriptblock]$args[0])"}
_m{sin((45*(_m{pi/180})))}

$_m = [math]
$_m::sin((45*($_m::pi/180)))

--
Kiron

Oisin Grehan

unread,
Aug 12, 2007, 10:09:00 PM8/12/07
to

Why write functions when PowerShell can do it for you ;-)

--- begin import.ps1 ---

param([type]$type = $(throw "need a type!"))

$type | gm -static | ? {$_.membertype -eq "method" } | % {
$func = "function:$($_.name)"
if (test-path $func) { remove-item $func }
new-item $func -value "[$($type.fullname)].InvokeMember('$($_.name)',
'Public,Static,InvokeMethod,DeclaredOnly', `$null, `$null, `$args[0])"
}

--- end import.ps1 ---

# dot source for interactive use

PS > . .\import.ps1 ([math])

CommandType Name Definition
----------- ---- ----------
Function Abs [System.Math].InvokeMember('Abs', ' ...
Function Acos [System.Math].InvokeMember('Acos', ...
Function Asin [System.Math].InvokeMember('Asin', ...
Function Atan [System.Math].InvokeMember('Atan', ...
Function Atan2 [System.Math].InvokeMember('Atan2', ...
Function BigMul [System.Math].InvokeMember('BigMul' ...
Function Ceiling [System.Math].InvokeMember('Ceiling ...
Function Cos [System.Math].InvokeMember('Cos', '
...

PS > abs -1
1

PS > max @(2,4)
4

You could easily rewrite this to be a bit like VB's "With" keyword:

PS > with ([math]) { sin 3; }

Hope this helps,

- Oisin

Duncan Smith

unread,
Aug 13, 2007, 6:05:46 AM8/13/07
to

Thanks Oisin, I hadn't thought of dynamically building up the function
provider with the meta-data fom gm -static. Now that you mention it,
it works very well. I may borrow Kiron's Invoke-Expression method
too, to extend it so that it can return the properties like pi and e.

Can probably live without the 'With' keyword, was never a huge fan of
VB... ;-)

Many thanks,

Duncan

Kiron

unread,
Aug 13, 2007, 1:30:23 PM8/13/07
to
To get member properties you can add either block of code, or both, to
Oisin's script. There may very well be a better way since I'm a .Net
apprentice:

# Properties as functions:
$type | gm -static | ? {$_.membertype -eq "property" } | % {


$func = "function:$($_.name)"
if (test-path $func) { remove-item $func }
new-item $func -value "[$($type.fullname)].InvokeMember('$($_.name)',

'Public,Static,GetField,DeclaredOnly', `$null, `$null, `$null)"
}

# Properties as variables:
$type | gm -static | ? {$_.membertype -eq "property" } | % {
$var = "variable:$($_.name)"
if (test-path $var) { remove-item $var }
new-item $var -value (Invoke-Expression "[$($type.fullname)]::$($_.name)")
}

--
Kiron

Kiron

unread,
Aug 13, 2007, 1:24:27 PM8/13/07
to
Thanks Oisin, great tip!

--
Kiron

Kiron

unread,
Aug 18, 2007, 2:26:32 PM8/18/07
to
Some functions generated by the script are not functional.
I tried Reverse after importing them from [array] but got this exception:

>$a = ql a b c d e
>"$a"
a b c d e
>reverse($a)
Exception calling "InvokeMember" with "5" argument(s): "Method
'System.Array.Re
verse' not found."
At line:1 char:28
+ [System.Array].InvokeMember( <<<< 'Reverse',

Got similar exceptions for Sort and others, also for [string]'s Join.

--
Kiron

Oisin Grehan

unread,
Aug 18, 2007, 6:01:02 PM8/18/07
to

Hi Kiron,

I think you misundertand the mechanics of Reverse:

PS 1> . .\import.ps1 ([array])
...
PS 2> $arr = @(1,2,3,4,5)
PS 3> Reverse (,$arr)
PS 4> $arr
5
4
3
2
1
PS 5> Reverse (,$arr)
1
2
3
4
5

Reverse acts ON the array, it doesn't return a value. Also, note the
use of the prefixed comma array constructor to match the expected
signature for Reverse.

The exception you get is what happens when you don't provide the
correct signature. It's tricky sometimes ;-)

Keith Hill

unread,
Aug 19, 2007, 9:46:20 PM8/19/07
to
"Kiron" <Ki...@HighPlainsDrifter.com> wrote in message
news:89EFF765-9D68-4685...@microsoft.com...
>I was expecting that Reverse() would accept args as [array]::Reverse()
>does,
> same for Sort() and [string]'s Join().
> By the way could you suggest a .Net book, or books, that give you that
> sort
> of insight, although experience must of taught you, you must have come
> across a good .Net book.
> Thanks again Oisin.

I really like Jeff Richter's "CLR via C# - second edition".

--
Keith

Kiron

unread,
Aug 20, 2007, 12:14:26 AM8/20/07
to
Thanks Keith.

--
Kiron

0 new messages