Skip to content

Commit aa8fb99

Browse files
switch to hashtable
1 parent 4877e45 commit aa8fb99

File tree

1 file changed

+11
-20
lines changed

1 file changed

+11
-20
lines changed

Get-PropertyType.ps1

+11-20
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@
106106
Where-Object{ -not $excludeProperty -or $excludeProperty -notcontains $_ }
107107
}
108108
} #Get-PropertyOrder
109+
110+
$Output = @{}
109111
}
110112

111113
Process {
@@ -118,10 +120,7 @@
118120

119121
#loop through every property in this one object
120122
foreach($prop in $props){
121-
122-
#set up a variable name we will use to store an array of unique types for this property
123-
$varName = "_My$prop"
124-
123+
125124
#try to get the property type. If it's null, say so
126125
Try{
127126
$type = $obj.$prop.gettype().FullName
@@ -130,35 +129,27 @@
130129
$type = $null
131130
}
132131

133-
#init currentvalue to null, might not need this
134-
$currentValue = $null
135-
136-
#check to see if we already have an array of types for this property. Set current value in the logic
137-
if(-not ($currentValue = Get-Variable $varName -ErrorAction SilentlyContinue -ValueOnly)){
132+
#check to see if we already have types for this prop
133+
if(-not $Output.ContainsKey($prop)){
138134

139-
#we don't have an array yet. Start one, put the type in it, give it a description we can use later
135+
#we don't have an array yet. Start one, put the type in it
140136
$List = New-Object System.Collections.ArrayList
141137
[void]$List.Add($type)
142-
Set-Variable -name $varName -value $List -force -Description "_MyProp"
138+
$Output.Add($prop, $List)
143139

144140
}
145141
else{
146-
if($currentValue -notcontains $type){
142+
if($Output[$prop] -notcontains $type){
147143

148144
#type isn't in the array yet, add it
149-
[void]$currentValue.Add($type)
150-
Set-Variable -name $varName -Value $currentValue -force -Description "_MyProp"
145+
[void]$output[$prop].Add($type)
151146
}
152147
}
153148
}
154149
}
155150
}
156151
End {
157-
158-
#get all the results, remove _My from their name
159-
Get-Variable -Scope 0 |
160-
Where-Object {$_.Description -eq "_MyProp"} |
161-
Select-Object -Property @{ label = "Name"; expression = {$_.name -replace "^_My",""} }, Value
162-
152+
153+
$Output
163154
}
164155
}

0 commit comments

Comments
 (0)