Implements class getter and setter accessors.
Use-ClassAccessors
[-Class <String[]>]
[-Property <String>]
[-Force]
[<CommonParameters>]The Use-ClassAccessors cmdlet updates script property of a class from the getter and setter methods. Which are also known as accessors or mutator methods.
The getter and setter methods should use the following syntax:
[<type>] get_<property name>() {
return <variable>
}or:
[Object] get_<property name>() {
return [<Type>]<variable>
}Note
Any (single) item array will be unrolled if the [Object] type is used for the getter method.
set_<property name>(<variable>) {
<code>
}Note
A setter accessor requires a getter accessor to implement the related property.
Note
In most cases, you might want to hide the getter and setter methods using the hidden keyword
on the getter and setter methods.
The following example defines a getter and setter for a value property
and a readonly property for the type of the type of the contained value.
Install-Script -Name Use-ClassAccessors
Class ExampleClass {
hidden static ExampleClass() { Use-ClassAccessors }
hidden $_Value
hidden [Object] get_Value() {
return $this._Value
}
hidden set_Value($Value) {
$this._Value = $Value
}
hidden [Type]get_Type() {
if ($Null -eq $this.Value) { return $Null }
else { return $this._Value.GetType() }
}
}
$Example = [ExampleClass]::new()
$Example.Value = 42 # Set value to 42
$Example.Value # Returns 42
$Example.Type # Returns [Int] type info
$Example.Type = 'Something' # Throws readonly errorSpecifies the class from which the accessor need to be initialized. Default: The class from which this function is invoked (by its static initializer).
Name: -Class
Aliases: # None
Type: [String[]]
Value (default): # Undefined
Parameter sets: # All
Mandatory: False
Position: # Named
Accept pipeline input: False
Accept wildcard characters: FalseFilters the property that requires to be (re)initialized. Default: All properties in the given class
Name: -Property
Aliases: # None
Type: [String]
Value (default): # Undefined
Parameter sets: # All
Mandatory: False
Position: # Named
Accept pipeline input: False
Accept wildcard characters: FalseIndicates that the cmdlet reloads the specified accessors, even if the accessors already have been defined for the concerned class.
Name: -Force
Aliases: # None
Type: [SwitchParameter]
Value (default): # Undefined
Parameter sets: # All
Mandatory: False
Position: # Named
Accept pipeline input: False
Accept wildcard characters: False