2
私は最近、私はこの素晴らしいガイドにhttps://xainey.github.io/2016/powershell-classes-and-concepts/#methodspowershell 5クラスのGetter/Setter関数をオーバーライドできますか?
を以下ましたが、get_x
とset_x
メソッドをオーバーライドすることが可能である場合、私は思っていたのPowerShell 5.持つクラスを作成し始めました。
例:
Class Foobar2 {
[string]$Prop1
}
$foo = [Foobar2]::new()
$foo | gm
Name MemberType Definition
---- ---------- ----------
Equals Method bool Equals(System.Object obj)
GetHashCode Method int GetHashCode()
GetType Method type GetType()
ToString Method string ToString()
Prop1 Property string Prop1 {get;set;}
私は私のカスタムGet
とSet
メソッドを使用するよりもプロパティにアクセスするために他のために容易になるだろうと思うので、私はそれを行うしたいと思います。残念ながら
Class Foobar {
hidden [string]$Prop1
[string] GetProp1() {
return $this.Prop1
}
[void] SetProp1([String]$Prop1) {
$this.Prop1 = $Prop1
}
}
これは、クラスのコンストラクタ内に新しい変数を追加するとうまくいくようです。残念ながら、[Gist Example](https://gist.github.com/OCram85/03ce8c0f881477c835e3fdfc279dfed7)のような既存のプロパティをオーバーライドできません。 – OCram85
@ OCram85いいえ、私のような別の名前の隠れたバッキングフィールドを使用する必要があります例 –