5
我需要$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()
錯字:_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中缺少。 –