Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell singleton和$此行为_Powershell_Oop_Singleton - Fatal编程技术网

Powershell singleton和$此行为

Powershell singleton和$此行为,powershell,oop,singleton,Powershell,Oop,Singleton,我正处于一个主要的重构和PS OOP教育的阵痛中,当前的主题是单例。我有一些程序状态数据,我想在单例中提供,我看到一些奇怪的行为 这适用于前两个写行,但在引用第二个类的第三个写行上挂起 class PxStatus { static [PxStatus] $singleton = $null [string]$Context = 'machine' [datetime]$StartTime = (Get-Date) static [PxStatus] Get()

我正处于一个主要的重构和PS OOP教育的阵痛中,当前的主题是单例。我有一些程序状态数据,我想在单例中提供,我看到一些奇怪的行为

这适用于前两个写行,但在引用第二个类的第三个写行上挂起

class PxStatus {
    static [PxStatus] $singleton = $null
    [string]$Context = 'machine'
    [datetime]$StartTime = (Get-Date)

    static [PxStatus] Get() {
        if ([PxStatus]::singleton -eq $null) {
            [PxStatus]::singleton = [PxStatus]::new()
        }

        return [PxStatus]::singleton
    }
}

class pxLogFile {
    # Properties
    static [pxLogFile] $singleton = $null
    [string] $nameSeed = "PxTools $(([PxStatus]::Get()).Context) context"
    [string] $path = "$env:TEMP\$(([pxLogFile]::Get()).nameSeed) $((get-date).toString('yyyy-MM-dd-hh-mm-ss')).log"

    static [pxLogFile] Get() {
        if ([pxLogFile]::singleton -eq $null) {
            [pxLogFile]::singleton = [pxLogFile]::new()

        return [pxLogFile]::singleton
    }
}



CLS
Write-Host "$(([PxStatus]::Get()).Context)"
Write-Host "$(([PxStatus]::Get()).StartTime)"
Write-Host "$(([pxLogFile]::Get()).path)"
但是,如果我在构造函数中处理路径,就像这样,它就可以工作

class PxStatus {
    static [PxStatus] $singleton = $null
    [string]$Context = 'machine'
    [datetime]$StartTime = (Get-Date)

    static [PxStatus] Get() {
        if ([PxStatus]::singleton -eq $null) {
            [PxStatus]::singleton = [PxStatus]::new()

            #([PxStatus]::singleton).Context = 'machine'
            #([PxStatus]::singleton).StartTime = Get-Date
        }

        return [PxStatus]::singleton
    }
}

class pxLogFile {
    # Properties
    static [pxLogFile] $singleton = $null
    [string] $nameSeed = "PxTools $(([PxStatus]::Get()).Context) context"
    [string] $path = $null

    static [pxLogFile] Get() {
        if ([pxLogFile]::singleton -eq $null) {
            [pxLogFile]::singleton = [pxLogFile]::new()

            ([pxLogFile]::singleton).Path = "$env:TEMP\$(([pxLogFile]::Get()).nameSeed) $((get-date).toString('yyyy-MM-dd-hh-mm-ss')).log"
        }

        return [pxLogFile]::singleton
    }
}



CLS
Write-Host "$(([PxStatus]::Get()).Context)"
Write-Host "$(([PxStatus]::Get()).StartTime)"
Write-Host "$(([pxLogFile]::Get()).path)
我假设问题在于访问同一个单例中的属性,就像我对同名种子所做的那样。或者,我也可以在属性定义中使用它

[string] $path = "$env:TEMP\$($this.nameSeed) $((get-date).toString('yyyy-MM-dd-hh-mm-ss')).log"
老实说,这本书更具可读性。我只是想知道第一个失败的原因到底是什么?我是否通过不使用
$this

调用
[pxLogFile]::Get()
内部嵌套来创建某种奇怪的循环条件

[string] $path = "$env:TEMP\$(([pxLogFile]::Get()).nameSeed) $((get-date).toString('yyyy-MM-dd-hh-mm-ss')).log"
导致无限递归,最终导致堆栈溢出

在初始化
$path
时,只需直接参考另一个静态属性
$nameed

class pxLogFile {
    # Properties
    static [pxLogFile] $singleton = $null
    [string] $nameSeed = "PxTools $(([PxStatus]::Get()).Context) context"
    # Refer to $nameSeed instead of to [pxLogFile]::Get()).nameSeed
    [string] $path = "$env:TEMP\$($nameSeed) $((get-date).toString('yyyy-MM-dd-hh-mm-ss')).log"

    static [pxLogFile] Get() {
        if ($null -eq [pxLogFile]::singleton) {
            [pxLogFile]::singleton = [pxLogFile]::new()
        }
        return [pxLogFile]::singleton
    }
}