Open nohwnd opened 3 months ago
fflaten wrote:
Experimented a little with this. Example:
# root of Format2.ps1
$script:defaultDisplayPropertyCache = [System.Collections.Generic.Dictionary[type,string[]]]::new()
function Get-DisplayProperty2 ($Object) {
$propertyMap = @{
[System.Diagnostics.Process] = 'Id', 'Name'
}
$type = $Object.GetType()
$properties = $propertyMap[$type]
if ($null -ne $properties) { return $properties }
# Fallback to DefaultDisplayPropertySet if defined for type
if ($script:defaultDisplayPropertyCache.TryGetValue($type,[ref]$properties)) { return $properties }
# Alterantive to enable inheritance: Loop $Object.PSTypeNames with (Get-TypeData $typename).DefaultDisplayPropertySet.ReferencedProperties
# Would be slower and not sure if any types work this way
if (($standardMembers = $Object.psobject.Members['PSStandardMembers']) -and
($defaultDisplayPropSet = $standardMembers.psobject.Members['DefaultDisplayPropertySet'])) {
# Is this a live reference we need to break? CopyTo new stringarray?
$properties = $defaultDisplayPropSet.ReferencedPropertyNames -as [string[]]
}
# Always cache to skip repeating PSStandardMembers check
$script:defaultDisplayPropertyCache.Add($type, $properties)
$properties
}
Thoughts:
DirectoryInfo
do not have standard member set, so needs to go in the $propertyMap
to avoid infinite loop due to Root
-property.Get-DisplayProperty2
could exclude properties that were different? Should be visible with -Verbose
but still?
Infinite loop for
FileInfo
andDirectoryInfo
due toRoot
properties inDirectoryInfo
. Should we restrict depth as a general fix in addition to specialcasingDirectoryInfo
inGet-DisplayProperty2
?_Originally posted by @fflaten in https://github.com/pester/Pester/pull/2428#discussion_r1606553664_