12

How can I implement a get/set property with PowerShell class? Please have a look on my example below:

Class TestObject
{
  [DateTime]$StartTimestamp = (Get-Date)
  [DateTime]$EndTimestamp = (Get-Date).AddHours(2)

  [TimeSpan] $TotalDuration {
    get {
      return ($this.EndTimestamp - $this.StartTimestamp)
    }
  }
  
  hidden [string] $_name = 'Andreas'
  [string] $Name {
    get {
      return $this._name
    }
    set {
      $this._name = $value
    }
  }
}

New-Object TestObject
phuclv
  • 37,963
  • 15
  • 156
  • 475
Augustin Ziegler
  • 163
  • 1
  • 1
  • 7

2 Answers2

14

You can use Add-Member ScriptProperty to achieve a kind of getter and setter:

class c {
    hidden $_p = $($this | Add-Member ScriptProperty 'p' `
        {
            # get
            "getter $($this._p)"
        }`
        {
            # set
            param ( $arg )
            $this._p = "setter $arg"
        }
    )
}

Newing it up invokes the initializer for $_p which adds scriptproperty p:

PS C:\> $c = [c]::new()

And using property p yields the following:

PS C:\>$c.p = 'arg value'
PS C:\>$c.p
getter setter arg value

This technique has some pitfalls which are mostly related to how verbose and error-prone the Add-Member line is. To avoid those pitfalls, I implemented Accessor which you can find here.

Using Accessor instead of Add-Member does an amount of error-checking and simplifies the original class implementation to this:

class c {
    hidden $_p = $(Accessor $this {
        get {
            "getter $($this._p)"
        }
        set {
            param ( $arg )
            $this._p = "setter $arg"
        }
    })
}
alx9r
  • 3,675
  • 4
  • 26
  • 55
  • This works great for me, but can you help me understand why `hidden $_p = $($this | Add-Member...)` works? I thought that assignment would only be invoked as a default value for the property, so if I was initializing it in a constructor the ScriptProperty wouldn't be added. – NReilingh Mar 10 '19 at 16:52
  • @NReilingh As I understand it, the scriptblocks on the RHS of the property definition are invoked, followed by the constructor. What happens during initialization/construction is unspecified and undocumented. I'm relying on [this](https://github.com/alx9r/ToolFoundations/blob/master/EnvironmentTests/classTests.ps1#L46-L60) and [this](https://github.com/alx9r/ToolFoundations/blob/master/EnvironmentTests/classTests.ps1#L583-L598) test to confirm the initialization/construction sequence. – alx9r Mar 10 '19 at 17:03
  • Ah, okay. So that should mean you could also define these accessors inside the constructor if you don’t mind the code separation, right? – NReilingh Mar 10 '19 at 17:06
  • @NReilingh As far I understand, yes. – alx9r Mar 10 '19 at 17:12
1

Here's how I went about it

  [string]$BaseCodeSignUrl;   # Getter defined in __class_init__.  Declaration allows intellisense to pick up property
  [string]$PostJobUrl;        # Getter defined in __class_init__.  Declaration allows intellisense to pick up property
  [hashtable]$Headers;        # Getter defined in __class_init__.  Declaration allows intellisense to pick up property
  [string]$ReqJobProgressUrl; # Getter defined in __class_init__.  Declaration allows intellisense to pick up property

  # Powershell lacks a way to add get/set properties.  This is a workaround
  hidden $__class_init__ = $(Invoke-Command -InputObject $this -NoNewScope -ScriptBlock {
    $this | Add-Member -MemberType ScriptProperty -Name 'BaseCodeSignUrl' -Force -Value {
      if ($this.Production) { [CodeSign]::CodeSignAPIUrl } else { [CodeSign]::CodeSignTestAPIUrl }
    }
    $this | Add-Member -MemberType ScriptProperty -Name 'PostJobUrl' -Force -Value {
      "$($this.BaseCodeSignUrl)/Post?v=$([CodeSign]::ServiceApiVersion)"
    }
    $this | Add-Member -MemberType ScriptProperty -Name 'Headers' -Force -Value {
      @{
        _ExpireInMinutes=[CodeSign]::Timeout.Minutes;
        _CodeSigningKey=$this.Key;
        _JobId=$this.JobId;
        _Debug=$this.Dbg;
        _Token=$this.Token;
      }
    }
    $this | Add-Member -MemberType ScriptProperty -Name 'ReqJobProgressUrl' -Force -Value {
      "$($this.BaseCodeSignUrl)Get?jobId=$($this.JobId)"
    }
  });
Derek Ziemba
  • 2,467
  • 22
  • 22