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
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
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
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
# 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
>$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
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 ;-)
I really like Jeff Richter's "CLR via C# - second edition".
--
Keith
--
Kiron