是否有正確/最好/任何方式使用PowerShell 5.0中的類結構來構建單例類?我已經試過這樣的事情:Powershell 5類和單身人士
class ScannerGeometry
{
[int] $EEprom; # read eeprom
# ....
ScannerGeometry()
{
# ... fetch calibration from instrument
}
[ScannerGeometry] Instance() {
if ($this -eq $null)
{
$this = [ScannerGeometry]::new()
}
return $this
}
}
並賦予它的東西,如:
$scanner = [ScannerGeometry]::Instance()
唉,我收到的Method invocation failed because [ScannerGeometry] does not contain a method named 'Instance'.
而且運行時錯誤,可能使一個構造函數(或任何其他方法)私人在PS5類?
呵呵,是通過「獲取幫助about_Classes -Full」今天早些時候略讀:) –