PowerShell / PowerShell/PowerShell

GetType() errors out on PSMemberSet

未关闭
#19,789 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

Needs-Triage
主要语言
C#
星标
55.5k
派生
8.5k
平均合并
1 天 2 小时
30 天内合并 PR
88

描述

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

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

使用 issue 中的 PowerShell 命令重现 PSMemberSet GetType() 失败,然后从 System.Management.Automation 堆栈跟踪中的 PSInvokeMemberBinder.InvokeAdaptedMember 开始。跟踪 PSStandardMembers 如何在 PSCustomObject 上公开,并验证在成员元数据仍与预期输出一致的情况下 GetType() 可以正常工作。

由索引模型根据 Issue 内容生成。

评估

技术栈
csharp, shell
领域
cli
Issue 类型
缺陷
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
35/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。