Home > Enterprise >  PowerShell Get List of base class properties
PowerShell Get List of base class properties

Time:12-01

I have 2 classes. One base class and one child class. I now want to know all the properties that are located in the base class.

class MyBase
{
     [Int64]$BaseAttrib
     MyBase([Int64]$BaseAttrib)
     {
         $this.BaseAttrib = $BaseAttrib
     }
}

class MyExtended : MyBase
{
     [Int64]$ExtendedAttrib
     MyExtended([Int64]$BaseAttrib,[Int64]$ExtendedAttrib) : base($BaseAttrib)
     {
         $this.ExtendedAttrib = $ExtendedAttrib
     }
}

$O1=New-Object -TypeName "MyExtended" -ArgumentList (0,1)
#$o1 | Get-Member -Name "ExtendedAttrib" -MemberType Property
([MyBase]$o1) | Get-Member -Name "ExtendedAttrib" -MemberType Property
([MyBase]$o1).ExtendedAttrib

I would have expected that the last line does not work as the base class does not have an ExtendedAttrib property. When I do a get-member, it always returns the list for the child class and never for the base class.

If I do ([MyBase]$O1). IntelliSense only displays the properties that are defined in the base class. How can I get the same list?

Thx

CodePudding user response:

Try $O1.GetType().BaseType.GetProperties()

  • Related