在 PowerShell 中深度复制字典(哈希表) [英] Deep copy a dictionary (hashtable) in PowerShell

查看:52
本文介绍了在 PowerShell 中深度复制字典(哈希表)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Clone() 只做一个浅拷贝,如果没有一些样板代码包装序列化,似乎没有直接的方法在 C# 中做到这一点(你如何在 .NET 中对对象进行深拷贝(特别是 C#)?).在 Powershell 中是否有一种简单的方法可以在不引用外部库的情况下执行此操作?

Clone() only does a shallow copy and it seems there is no straightforward way to do this in C# without a bit of boilerplate code wrapping serialization (How do you do a deep copy of an object in .NET (C# specifically)?). Is there a simple way to do this in Powershell without referencing external libraries ?

推荐答案

当你启动 shell 时,你需要的所有库都在那里,所以它只是按照你的链接实现深层复制.

All the libraries you need are there when you start the shell so it's just to implement the deep copy as per your link.

function Clone-Object {
    param($DeepCopyObject)
    $memStream = new-object IO.MemoryStream
    $formatter = new-object Runtime.Serialization.Formatters.Binary.BinaryFormatter
    $formatter.Serialize($memStream,$DeepCopyObject)
    $memStream.Position=0
    $formatter.Deserialize($memStream)
}

这篇关于在 PowerShell 中深度复制字典(哈希表)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆