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

I need $this to work inside a static class! How to do it? Any workaround? I analyzed the return of Get-PSCallStack in the context of the class and did not find anything useful.

I need this to (a) log and (b) call other static methods of the 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() 
+5
source share
1 answer

Static classes do not have a this pointer. See MSDN

Static member functions, since they exist at the class level and not as part of an object, do not have a pointer. It is a mistake to refer to this in the static method.

You must call the method by class name.

+1
source

All Articles