首先有一个名为hash_item_tpl的Hashtable。想用这个做一个模版
在随后的循环中新建名为hash_item的Hashtable
由于Hashtable中没有Copy的办法,而Clone只能操作浅表。所以在随后的循环中想用以下的方法来给hash_item赋值:
Hashtable hash_item = new Hashtable();
foreach(System.Collections.DictionaryEntry d in hash_item_tpl)
{
string str_item_code = d.Key.ToString();
//d.Value存储的是一个整形数组
int[] ary_item_val = (int[])d.Value;
hash_item.Add(str_item_code,ary_item_val);
}
但在随后对hash_item的元素操作时发现,hash_item的元素貌似还是hash_item_tpl的引用
对hash_item元素的修改,直接表现在hash_item_tpl的元素中。期待解决办法

解决方案 »

  1.   

    Hashtable hash_item = new Hashtable();
    foreach(System.Collections.DictionaryEntry d in hash_item_tpl)
    {
    string str_item_code = d.Key.ToString();
    //d.Value存储的是一个整形数组
    int[] ary_item_val = (int[])d.Value;
             int[] ary_temp = new int[ary_item_val.length];
             ary_item_val.CopyTo( ary_temp , 0);
    hash_item.Add(str_item_code,ary_temp);
    }
      

  2.   

    使用强制类型转换成ICloneable接口类型后再调用Clone()
      

  3.   

    hdt(倦怠),惭愧啊,自己居然没有弄清引用和Copy的区别。哎,VB转C#的痛处啊 :)mysterious(空折枝) 的方案也不错,查了下文档ICloneable接口确实可以定义是deep copy还是shallow copy,不过由于目前手头的项目比较紧,暂时没空测试。谢2位兄弟,散分,结贴