Please, observe:
C:\> ''|Get-Member |? { $_.MemberType -eq 'ParameterizedProperty' }
TypeName: System.String
Name MemberType Definition
---- ---------- ----------
Chars ParameterizedProperty char Chars(int index) {get;}
C:\>
This is a very weird property. First of all it is added by Powershell, next it contains an infinite recursive property:
C:\> ''.Chars
IsSettable : False
IsGettable : True
OverloadDefinitions : {char Chars(int index) {get;}}
TypeNameOfValue : System.Char
MemberType : ParameterizedProperty
Value : char Chars(int index) {get;}
Name : Chars
IsInstance : True
C:\> ''.Chars.Value
IsSettable : False
IsGettable : True
OverloadDefinitions : {char Chars(int index) {get;}}
TypeNameOfValue : System.Char
MemberType : ParameterizedProperty
Value : char Chars(int index) {get;}
Name : Chars
IsInstance : True
C:\> ''.Chars.GetHashCode()
56544304
C:\> ''.Chars.Value.GetHashCode()
34626228
C:\> ''.Chars.Value.Value.GetHashCode()
3756075
C:\> ''.Chars.Value.Value.Value.GetHashCode()
49108342
C:\> ''.Chars.Value.Value.Value.Value.GetHashCode()
62340979
C:\> ''.Chars.Value.Value.Value.Value.Value.GetHashCode()
24678148
C:\>
The hash code is different every time, so it must be dynamically generated.
Why do I care? I am trying to use a Newtonsoft.Json PowerShell module from PSGallery and it chokes on this property, but only when run in Desktop PowerShell (5.1), not the Core (7.0.3). The problem is that I do not have a minimal reproduction, the input object is quite large. The error I get is:
ConvertTo-JsonNewtonsoft : Exception calling "SerializeObject" with "2" argument(s): "Self referencing loop detected for property 'Value' with type 'System.Management.Automation.PSParameterizedProperty'. Path 'environments[4].conditions.name.Chars'."
No such problem exists in PS Core.
Can someone explain to me what is this property, why we need it and how can we get rid of it?
EDIT 1
I guess it is a problem with the Newtonsoft.Json
module. Observe:
[DBG]> [pscustomobject]@{ a = 1} | ConvertTo-Json
{
"a": 1
}
[DBG]> [pscustomobject]@{ a = 1} | ConvertTo-JsonNewtonsoft
{
"CliXml": "<Objs Version=\"1.1.0.1\" xmlns=\"http://schemas.microsoft.com/powershell/2004/04\">\r\n <Obj RefId=\"0\">\r\n <TN RefId=\"0\">\r\n
<T>System.Management.Automation.PSCustomObject</T>\r\n <T>System.Object</T>\r\n </TN>\r\n <ToString>@{a=1}</ToString>\r\n <Obj RefId=\"1\">\r\n <TNRef RefId=\"0\" />\r\n <MS>\r\n <I32 N=\"a\">1</I32>\r\n </MS>\r\n </Obj>\r\n <MS>\r\n <I32 N=\"a\">1</I32>\r\n </MS>\r\n </Obj>\r\n</Objs>"
}
[DBG]>
It is unable to properly interpret powershell objects. Makes it unusable.