samedi 20 août 2016

How to get current class name/object inside PowerShell static class method?

I need $this to work inside static class! How to achieve that? Any workaround? I have analyzed return of Get-PSCallStack in class context and found nothing useful.

I need this for (a) logging and for (b) calling other static methods of same class without mentioning its name again and again.

Sample code (PowerShell v5):

class foo {
    static [void]DoSomething() {
        [foo]::DoAnything()  #works

        #$this.DoAnything   #not working

        $static_this = [foo]
        $static_this::DoAnything() #works

    }
    static [void]DoAnything() {
        echo "Done"
    }
}

[foo]::DoSomething()





Aucun commentaire:

Enregistrer un commentaire