Windows Vista Forums

static methods
  1. #1


    Doug Guest

    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

  2. #2


    Nigel Sharples Guest

    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

static methods problems?

Similar Threads
Thread Thread Starter Forum Replies Last Post
get help on methods and properties William PowerShell 3 22 Oct 2008
Static Events Used By Static Classes Mike .NET General 7 05 Mar 2008
Access a static member on a nested static class. matthew.ashton@gmail.com PowerShell 3 28 Feb 2007
Access a static member on a nested static class. matthew.ashton@gmail.com PowerShell 0 28 Feb 2007
Output Methods THG PowerShell 6 02 Jul 2006