Why I cannot find where a property is defined in PowerShell? where is the source code of the PSObject class?

132 Views Asked by At

I want to find the PSObject.cs file in Github, but I cannot find it. And I haven't find any file containing definition of this class (System.Management.Automation.PSObject). In source code of many other classes of PowerShell, I found that the PSObject class is used widely, but many properties or methods of the PSObject class are not documented in offical document of it.

The reason I want to find the source of PSObject is, in the following code, even the statements in the part 2 work correctly, but both the Name and the Age properties are not included in the output of the two statements in the part 3.

# part 1
$obj = [pscustomobject] @{Name = "Joe"; Age = 42}

# part 2
$obj.psextended.Name  # "Joe"
$obj.Name  # "Joe"
$obj.psextended.Age  # 42
$obj.Age  # 42

# part 3
$obj.GetType().GetMembers() | select Name, MemberType  
$obj.psobject.GetType().GetMembers() | select Name, MemberType

The output of the first statement in part 3 is:

Name        MemberType
----        ----------
ToString        Method
GetType         Method
Equals          Method
GetHashCode     Method

The output of the second statement in part 3 is:

Name                     MemberType
----                     ----------
get_BaseObject               Method
get_Members                  Method
get_Properties               Method
get_Methods                  Method
get_ImmediateBaseObject      Method
get_TypeNames                Method
op_Implicit                  Method
op_Implicit                  Method
op_Implicit                  Method
op_Implicit                  Method
op_Implicit                  Method
AsPSObject                   Method
ToString                     Method
ToString                     Method
Copy                         Method
CompareTo                    Method
Equals                       Method
GetHashCode                  Method
GetObjectData                Method
GetType                      Method
.ctor                   Constructor
.ctor                   Constructor
.ctor                   Constructor
BaseObject                 Property
Members                    Property
Properties                 Property
Methods                    Property
ImmediateBaseObject        Property
TypeNames                  Property
AdaptedMemberSetName          Field
ExtendedMemberSetName         Field
BaseObjectMemberSetName       Field

It seems that these two properties are not defined neither in $obj (a PSCustomObject) or its wrapper (a PSObject).

So, where the Name and the Age properties are defined? What happened when the statements in part 2 run? In other words, What happened inside the PowerShell ETS? Why there is not a "Name" property defined in $obj or $obj.psobject, but $obj.Name works?

And, where can I find the real complete source code of the PSObject class?

0

There are 0 best solutions below