Created
December 6, 2016 07:55
-
-
Save OCram85/03ce8c0f881477c835e3fdfc279dfed7 to your computer and use it in GitHub Desktop.
Creating Getter/Setter method within powershell class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Class FooBar { | |
[string]$Prop1 | |
Foobar() { | |
$this | Add-Member -Name Prop1 -MemberType ScriptProperty -Value { | |
# This is the getter | |
return $this.Prop1 | |
} -SecondValue { | |
param($value) | |
# This is the setter | |
$this.Prop1 = $value | |
} -Force | |
} | |
} | |
$FooBar = [Foobar]::new() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This breaks if you try to access the property with someting like
$FooBar.Prop1
or$FooBar.Prop1 = "This doesn't work..."