Skip to content

Commit d86320b

Browse files
committed
Remove DeepCopy-Object entirely
As it was only used once, and the place where it was used could just deep copy in place by adding properties to the clone.
1 parent 167761d commit d86320b

File tree

2 files changed

+5
-42
lines changed

2 files changed

+5
-42
lines changed

GitHubCore.ps1

+5-2
Original file line numberDiff line numberDiff line change
@@ -1059,8 +1059,8 @@ filter ConvertTo-SmarterObject
10591059
}
10601060
elseif ($InputObject -is [PSCustomObject])
10611061
{
1062-
$clone = DeepCopy-Object -InputObject $InputObject
1063-
$properties = $clone.PSObject.Properties | Where-Object { $null -ne $_.Value }
1062+
$clone = [PSObject]::New()
1063+
$properties = $InputObject.PSObject.Properties | Where-Object { $null -ne $_.Value }
10641064
foreach ($property in $properties)
10651065
{
10661066
# Convert known date properties from dates to real DateTime objects
@@ -1087,6 +1087,9 @@ filter ConvertTo-SmarterObject
10871087
{
10881088
$property.Value = ConvertTo-SmarterObject -InputObject $property.Value
10891089
}
1090+
1091+
# Add the property to the clone.
1092+
$clone.PSObject.Properties.Add([PSNoteProperty]::New($property.Name, $property.Value))
10901093
}
10911094

10921095
Write-Output -InputObject $clone

Helpers.ps1

-40
Original file line numberDiff line numberDiff line change
@@ -360,46 +360,6 @@ function Write-InvocationLog
360360
Write-Log -Message "[$($Invocation.MyCommand.Module.Version)] Executing: $($Invocation.MyCommand) $($params -join ' ')" -Level Verbose
361361
}
362362

363-
function DeepCopy-Object
364-
{
365-
<#
366-
.SYNOPSIS
367-
Creates a deep copy of a serializable object.
368-
369-
.DESCRIPTION
370-
Creates a deep copy of a serializable object.
371-
By default, PowerShell performs shallow copies (simple references)
372-
when assigning objects from one variable to another. This will
373-
create full exact copies of the provided object so that they
374-
can be manipulated independently of each other, provided that the
375-
object being copied is serializable.
376-
377-
The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub
378-
379-
.PARAMETER InputObject
380-
The object that is to be copied. This must be serializable or this will fail.
381-
382-
.EXAMPLE
383-
$bar = DeepCopy-Object -InputObject $foo
384-
Assuming that $foo is serializable, $bar will now be an exact copy of $foo, but
385-
any changes that you make to one will not affect the other.
386-
387-
.RETURNS
388-
An exact copy of the PSObject that was just deep copied.
389-
#>
390-
[CmdletBinding()]
391-
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseApprovedVerbs", "", Justification="Intentional. This isn't exported, and needed to be explicit relative to Copy-Object.")]
392-
param(
393-
[Parameter(Mandatory)]
394-
[PSCustomObject] $InputObject
395-
)
396-
397-
$serialData = [System.Management.Automation.PSSerializer]::Serialize($InputObject, 64)
398-
$DeepCopiedObject = [System.Management.Automation.PSSerializer]::Deserialize($serialData)
399-
400-
return $DeepCopiedObject
401-
}
402-
403363
function New-TemporaryDirectory
404364
{
405365
<#

0 commit comments

Comments
 (0)