Why does the direct access of a static fail, but the indirect works? Note that the file loaded is valid in both examples.
Failure Using Direct To Static
class OpPrj {
[string] $ProjectPath
static [string] $configFile = 'settings.json';
[OpPrj] static GetSettings(){
return [OpPrj](Get-Content [OpPrj]::configFile | Out-String|ConvertFrom-Json);
}
Works By Assigning to Local
class OpPrj {
[string] $ProjectPath
static [string] $configFile = 'settings.json';
[OpPrj] static GetSettings(){
$file = [OpPrj]::configFile
Write-Host $file # outputs settings.json
return [OpPrj](Get-Content $file | Out-String | ConvertFrom-Json);
}
(Get-Content ([OpPrj]::configFile) | ...This is the way you need to reference static properties for any type (or enum!) when dealing with parameter binding, that is, wrapped in parens. I don't understand why the parser works that way here, but it does.$thisautomatic variable instead of[OpPrj]::configFile?$thisdoesn't exist for static members because they exist on the type not the object.