1

I would like to invoke a static method from an object based on the class of that object. For example, assume that you have the following class structure with static methods.


Class Super {
    static [string] getX() {
        return "X"
    }
}

Class Sub1 : Super {
    static [string] getX() {
        return "Sub1X"
    }

}
Class Sub2 : Super {
    static [string] getX() {
        return "Sub2X"
    }

}


$someSubclass = [Sub1]::new()

#I would like to invoke getX() from this instances classes static method.
$result = $someSubclass.GetType().getX()  #This (of course) does not work.

In this snippet above, I would like $result to contain the string "Sub1X". Any hints are appreciated.

1 Answer 1

1

Same as any other static member - use the :: static member operator:

$someSubClass = [Sub1]::new()
$result = $someSubClass::getX()
Sign up to request clarification or add additional context in comments.

1 Comment

@Hutch It's always easier once you know ^_^ hindsight is 20/20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.