gpt4 book ai didi

powershell - 如何在PowerShell静态类方法中获取当前类名/对象?

转载 作者:行者123 更新时间:2023-12-03 22:50:32 24 4
gpt4 key购买 nike

我需要$this在静态类中工作!如何做到这一点?任何解决方法?我分析了 Get-PSCallStack 的返回值在类里面,没有发现任何有用的东西。

我需要它用于 (a) 日志记录和 (b) 调用同一类的其他静态方法,而无需一次又一次地提及其名称。

示例代码(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()

最佳答案

静态类没有 this指针。见 MSDN

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



您必须按类名调用方法。

关于powershell - 如何在PowerShell静态类方法中获取当前类名/对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39055860/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com