如何获取PowerShell静态类方法中的当前类名/对象?
问题描述:
我需要$this
在静态类中工作!如何实现这一目标?任何解决方法?我已经分析了类上下文中返回的Get-PSCallStack
,发现没有用处。我需要(a)日志记录和(b)调用同一类的其他静态方法,而不再提及它的名字。如何获取PowerShell静态类方法中的当前类名/对象?
示例代码(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()
答
+0
感谢您迅速回复Pawel,但显然您的建议不能解决问题。正如我在我的问题中指出的,至少有一个解决方法'$ static_this = [foo]'。只是寻找更好的。 –
错字:_not available_ –
这在技术上是这个问题的重复:http://stackoverflow.com/questions/2113069/c-sharp-getting-its-own-class-name。像在PowerShell中那样,在C#中要求(对于静态方法而言)如此混乱。 –
@Chris Dent:'[System.Reflection.MethodBase] :: GetCurrentMethod()。DeclaringType'似乎不起作用。 'GetCurrentMethod()'结果的属性表明该方法是动态的。无论如何,在C#中,您可以从DoSomething中调用DoAnything,而无需完全引用static void DoSomething(){DoAnything(); }'。恰恰是我在PowerShell中缺少。 –