Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - static methods

Reply
 
Old 12-05-2006   #1 (permalink)
Doug


 
 

static methods

I have a DLL with two methods one static and one not.

public static int Test1(int p) { return 1; }
public int Test2(int p) { return 2;}

I use the [reflection.assembly] to load it no problem.
In script I create an array of strings with the method names and do a
New-Object on this class.

I want to loop through the array and call the methods on the object with the
same parameter.

It does not work and I don't know how to call a static method without this
syntax[Namespace.Class]::Method


My System SpecsSystem Spec
Old 12-05-2006   #2 (permalink)
Nigel Sharples


 
 

Re: static methods

If you have an instance of your object (let's assume $o) you could call a
static method like this:

$o.GetType()::Test1(1234)

If you have a string for the method name you could build an expression and
use invoke-expression:

$methodName = "Test1"
$arg = 1234
$line = '$o.GetType()::' + $methodName + '($arg)'
invoke-expression $line

You can get the list of static methods using Get-Member:

$o | Get-Member -static

--
Nigel Sharples [MSFT]
Windows PowerShell Test
Microsoft Corporation
This posting is provided "AS IS" with no warranties, and confers no rights.

"Doug" <Doug@discussions.microsoft.com> wrote in message
news:FE3C6232-FA59-4364-A0F7-7FDA9876F940@microsoft.com...
>I have a DLL with two methods one static and one not.
>
> public static int Test1(int p) { return 1; }
> public int Test2(int p) { return 2;}
>
> I use the [reflection.assembly] to load it no problem.
> In script I create an array of strings with the method names and do a
> New-Object on this class.
>
> I want to loop through the array and call the methods on the object with
> the
> same parameter.
>
> It does not work and I don't know how to call a static method without this
> syntax[Namespace.Class]::Method
>


My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
get help on methods and properties PowerShell
Static Events Used By Static Classes .NET General
Access a static member on a nested static class. PowerShell
Access a static member on a nested static class. PowerShell
Output Methods PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46