如何克隆Dictionary对象? [英] How do I clone a Dictionary object?

查看:524
本文介绍了如何克隆Dictionary对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在VBScript中有一个字典对象。如何将其中包含的所有对象复制到新的字典,即创建字典的克隆/副本?

I have a Dictionary object in VBScript. How can I copy all the objects contained in it to a new Dictionary, i.e. create a clone/duplicate of the dictionary?

推荐答案

创建一个新的字典对象,迭代原始字典中的键,并将这些键和相应的值添加到新的字典,如下所示:

Create a new Dictionary object, iterate through the keys in the original dictionary and adds these keys and the corresponding values to the new dictionary, like this:

Function CloneDictionary(Dict)
  Dim newDict
  Set newDict = CreateObject("Scripting.Dictionary")

  For Each key in Dict.Keys
    newDict.Add key, Dict(key)
  Next
  newDict.CompareMode = Dict.CompareMode

  Set CloneDictionary = newDict
End Function

在大多数情况下就足够了但是,如果您的原始字典包含对象,则必须执行深层克隆,也就是克隆这些对象。

This should be enough in most cases. However, if your original dictionary holds objects, you'll have to implement deep cloning, that is, clone these objects as well.

这篇关于如何克隆Dictionary对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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