PowerShell type accelerators: PSObject vs PSCustomObject

前端 未结 2 1392
闹比i
闹比i 2020-12-11 15:40

In PowerShell v3.0 PSCustomObject was introduced. It\'s like PSObject, but better. Among other improvements (e.g. property order being preserved),

相关标签:
2条回答
  • 2020-12-11 15:55

    [PSObject] and [PSCustomObject] are aliases for the same type - System.Management.Automation.PSObject. I can't say there's a good reason for it, but it is at least suggestive of two different purposes, and maybe that's reason enough.

    System.Management.Automation.PSObject is used to wrap objects. It was introduced to provide a common reflection api over any object that PowerShell wraps - .Net, WMI, COM, ADSI, or simple property bags.

    System.Management.Automation.PSCustomObject is just an implementation detail. When you create a PSObject, the PSObject must wrap something. For property bags, the object wrapped is System.Management.Automation.PSCustomObject.SelfInstance (an internal member.) This instance is hidden from normal use of PowerShell, the only way to observe it is with reflection.

    Property bags are created in multiple ways in PowerShell:

    $o1 = [pscustomobject]@{Prop1 = 42}
    $o2 = new-object psobject -Property @{Prop1 = 42 }
    

    Both $o1 and $o2 above will be an instance of PSObject, and the PSObject will wrap PSCustomObject.SelfInstance. PSCustomObject.SelfInstance is used internally in PowerShell to tell the difference between a simple property bag and wrapping any other object.

    0 讨论(0)
  • 2020-12-11 16:14

    Looking at the static methods:

    PS C:\> [PSCustomObject] | gm -Static -MemberType Method
    
    
    
       TypeName: System.Management.Automation.PSObject
    
    Name            MemberType Definition                                                        
    ----            ---------- ----------                                                        
    AsPSObject      Method     static psobject AsPSObject(System.Object obj)                     
    Equals          Method     static bool Equals(System.Object objA, System.Object objB)        
    new             Method     psobject new(), psobject new(System.Object obj)                   
    ReferenceEquals Method     static bool ReferenceEquals(System.Object objA, System.Object o...
    
    
    
    PS C:\> [System.Management.Automation.PSCustomObject] | gm -Static -MemberType Method
    
    
    
       TypeName: System.Management.Automation.PSCustomObject
    
    Name            MemberType Definition                                                        
    ----            ---------- ----------                                                        
    Equals          Method     static bool Equals(System.Object objA, System.Object objB)        
    ReferenceEquals Method     static bool ReferenceEquals(System.Object objA, System.Object o...
    

    The type accelerator has a couple of new static methods added. I suspect it's using one of those as the constructor.

    0 讨论(0)
提交回复
热议问题