PowerShell icon indicating copy to clipboard operation
PowerShell copied to clipboard

GetType() errors out on PSMemberSet

Open danielzgtg opened this issue 3 years ago • 0 comments

Prerequisites

Steps to reproduce

  1. Start with https://learn.microsoft.com/en-us/powershell/scripting/learn/deep-dives/everything-about-pscustomobject?view=powershell-7.3#using-defaultpropertyset-the-long-way
$myObject = [PSCustomObject]@{
PSTypeName = 'My.Object'
Name       = 'Kevin'
Language   = 'PowerShell'
State      = 'Texas'
}
$defaultDisplaySet = 'Name','Language'
$defaultDisplayPropertySet = New-Object System.Management.Automation.PSPropertySet('DefaultDisplayPropertySet',[string[]]$defaultDisplaySet)
$PSStandardMembers = [System.Management.Automation.PSMemberInfo[]]@($defaultDisplayPropertySet)
$MyObject | Add-Member MemberSet PSStandardMembers $PSStandardMembers
  1. See that the original $PSStandardMembers works when it's still a System.Management.Automation.PSPropertySet, except it strangely doesn't have System.Object as typename
'$PSStandardMembers:'
$PSStandardMembers
'---'
$PSStandardMembers.GetType()
$PSStandardMembers.psobject
  1. See that there is an unexpected error when trying to do the same thing on a pscustomobject where the added variable has been converted to a System.Management.Automation.PSMemberSet. I expect GetType() to work on all objects
'$myObject:'
$myObject.PSStandardMembers
'---'
$myObject.PSStandardMembers.GetType()
$myObject.psobject
$myObject.PSStandardMembers.psobject
  1. I discovered this problem while investigating a similar problem with this $myObject.PSStandardMembers failing to display as Format-List in $myObject.psobject.Members and displaying as the strange string PSStandardMembers {DefaultDisplayPropertySet} instead

Expected behavior

$PSStandardMembers:

ReferencedPropertyNames : {Name, Language}
MemberType              : PropertySet
Value                   : DefaultDisplayPropertySet {Name, Language}
TypeNameOfValue         : System.Management.Automation.PSPropertySet
Name                    : DefaultDisplayPropertySet
IsInstance              : True


---

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     PSMemberInfo[]                           System.Array


Members             : {int Length {get;}, long LongLength {get;}, int Rank {get;}, System.Object SyncRoot {get;}…}
Properties          : {int Length {get;}, long LongLength {get;}, int Rank {get;}, System.Object SyncRoot {get;}…}
Methods             : {Get, Set, Address, get_Length…}
ImmediateBaseObject : {DefaultDisplayPropertySet {Name, Language}}
BaseObject          : {DefaultDisplayPropertySet {Name, Language}}
TypeNames           : {System.Management.Automation.PSMemberInfo[], System.Array, System.Object}


$myObject:
PSStandardMembers {DefaultDisplayPropertySet}
---

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     PSMemberInfo[]                           System.Array


Members             : {string Name=Kevin, string Language=PowerShell, string State=Texas, PSStandardMembers {DefaultDisplayPropertySet}…}
Properties          : {string Name=Kevin, string Language=PowerShell, string State=Texas}
Methods             : {ToString, GetType, Equals, GetHashCode}
ImmediateBaseObject : 
BaseObject          : 
TypeNames           : {My.Object, System.Management.Automation.PSCustomObject, System.Object}


Members             : {DefaultDisplayPropertySet {Name, Language}}
Properties          : {[something here]}
Methods             : {[something here]}
ImmediateBaseObject : PSStandardMembers {DefaultDisplayPropertySet}
BaseObject          : PSStandardMembers {DefaultDisplayPropertySet}
TypeNames           : {System.Management.Automation.PSMemberSet, System.Object}


Actual behavior

$PSStandardMembers:

ReferencedPropertyNames : {Name, Language}
MemberType              : PropertySet
Value                   : DefaultDisplayPropertySet {Name, Language}
TypeNameOfValue         : System.Management.Automation.PSPropertySet
Name                    : DefaultDisplayPropertySet
IsInstance              : True


---

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     PSMemberInfo[]                           System.Array


Members             : {int Length {get;}, long LongLength {get;}, int Rank {get;}, System.Object SyncRoot {get;}…}
Properties          : {int Length {get;}, long LongLength {get;}, int Rank {get;}, System.Object SyncRoot {get;}…}
Methods             : {Get, Set, Address, get_Length…}
ImmediateBaseObject : {DefaultDisplayPropertySet {Name, Language}}
BaseObject          : {DefaultDisplayPropertySet {Name, Language}}
TypeNames           : {System.Management.Automation.PSMemberInfo[], System.Array, System.Object}


$myObject:
PSStandardMembers {DefaultDisplayPropertySet}
---
InvalidOperation: Method invocation failed because [System.Management.Automation.PSMemberSet] does not contain a method named 'GetType'.

Members             : {string Name=Kevin, string Language=PowerShell, string State=Texas, PSStandardMembers {DefaultDisplayPropertySet}…}
Properties          : {string Name=Kevin, string Language=PowerShell, string State=Texas}
Methods             : {ToString, GetType, Equals, GetHashCode}
ImmediateBaseObject : 
BaseObject          : 
TypeNames           : {My.Object, System.Management.Automation.PSCustomObject, System.Object}

Members             : {DefaultDisplayPropertySet {Name, Language}}
Properties          : {}
Methods             : {}
ImmediateBaseObject : PSStandardMembers {DefaultDisplayPropertySet}
BaseObject          : PSStandardMembers {DefaultDisplayPropertySet}
TypeNames           : {System.Management.Automation.PSMemberSet}


Error details

Exception             : 
    Type        : System.Management.Automation.RuntimeException
    ErrorRecord : 
        Exception             : 
            Type    : System.Management.Automation.ParentContainsErrorRecordException
            Message : Method invocation failed because [System.Management.Automation.PSMemberSet] does not contain a method named 'GetType'.
            HResult : -2146233087
        TargetObject          : GetType
        CategoryInfo          : InvalidOperation: (GetType:String) [], ParentContainsErrorRecordException
        FullyQualifiedErrorId : MethodNotFound
        InvocationInfo        : 
            ScriptLineNumber : 1
            OffsetInLine     : 1
            HistoryId        : -1
            Line             : $myObject.PSStandardMembers.GetType()
            PositionMessage  : At line:1 char:1
                               + $myObject.PSStandardMembers.GetType()
                               + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
            CommandOrigin    : Internal
        ScriptStackTrace      : at <ScriptBlock>, <No file>: line 1
    TargetSite  : 
        Name          : InvokeAdaptedMember
        DeclaringType : System.Management.Automation.Language.PSInvokeMemberBinder, System.Management.Automation, Version=7.3.4.500, Culture=neutral, 
PublicKeyToken=31bf3856ad364e35
        MemberType    : Method
        Module        : System.Management.Automation.dll
    Message     : Method invocation failed because [System.Management.Automation.PSMemberSet] does not contain a method named 'GetType'.
    Data        : System.Collections.ListDictionaryInternal
    Source      : System.Management.Automation
    HResult     : -2146233087
    StackTrace  : 
   at System.Management.Automation.Language.PSInvokeMemberBinder.InvokeAdaptedMember(Object obj, String methodName, Object[] args)
   at System.Dynamic.UpdateDelegates.UpdateAndExecute1[T0,TRet](CallSite site, T0 arg0)
   at System.Management.Automation.Interpreter.DynamicInstruction`2.Run(InterpretedFrame frame)
   at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
TargetObject          : GetType
CategoryInfo          : InvalidOperation: (GetType:String) [], RuntimeException
FullyQualifiedErrorId : MethodNotFound
InvocationInfo        : 
    ScriptLineNumber : 1
    OffsetInLine     : 1
    HistoryId        : -1
    Line             : $myObject.PSStandardMembers.GetType()
    PositionMessage  : At line:1 char:1
                       + $myObject.PSStandardMembers.GetType()
                       + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    CommandOrigin    : Internal
ScriptStackTrace      : at <ScriptBlock>, <No file>: line 1

Environment data

Name                           Value
----                           -----
PSVersion                      7.3.4
PSEdition                      Core
GitCommitId                    7.3.4
OS                             Linux 6.3.2-surface #1 SMP PREEMPT_DYNAMIC Fri May 12 15:00:39 UTC 2023
Platform                       Unix
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Visuals

No response

danielzgtg avatar Jun 13 '23 02:06 danielzgtg