C#对象数组CopyTo用于连接两个数组的价值观? [英] C# Object Array CopyTo links both arrays' values?

查看:192
本文介绍了C#对象数组CopyTo用于连接两个数组的价值观?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我有什么,我认为是一个简单的问题..或只是我是一个C#初学者的情况。

我有我从数据库填充自定义对象(clsScriptItem)阵列。一旦项目被加载,我想将它们备份到备份数组这样我就可以改变主阵列后归回信息。然而,当我使用CopyTo从到阵列复制,然后修改原始阵列,备份阵列也被改变了......我还以为只是CopyTo从复制值+结构从一个阵列到另一个。

 私人无效backupItems()
    {
        // lastSavedItems和物品的类型都是clsScriptItem的[]
        //(未显示声明)        lastSavedItems =新clsScriptItem [items.Length]        items.CopyTo(lastSavedItems,0);        //items[0].nexts[0]为2
        //lastSavedItems[0].nexts[0]为2        项[0] .nexts [0] =-1;        //items[0].nexts[0] -1
        //lastSavedItems[0].nexts[0]也是-1
    }

如何备份这些数据,而无需两个数组被'挂'??

更新:
我已经更新了备份功能本

 私人无效backupItems()
    {
        lastSavedItems =新clsScriptItem [items.Length]
        的for(int i = 0; I< items.Length;我++)
            lastSavedItems [I] =(clsScriptItem)项目[I] .Clone();        项[0] .nexts [0] =-1;    }

和我有更新正是如此我的课......

 公共类clsScriptItem:ICloneable
{    //其他声明...    对象ICloneable.Clone(){返回的clone(); }    公共clsScriptItem克隆()
    {
        回报((clsScriptItem)MemberwiseClone());
    }
}

** - 同样的事情正在发生。任何想法**


解决方案

 试试这个: - 公共静态的MyType [] DeepClone(的MyType [] OBJ)
     {
                使用(MemoryStream的毫秒=新的MemoryStream())
                {
                    BinaryFormatter的格式化=新的BinaryFormatter();
                    formatter.Serialize(MS,OBJ);
                    ms.Position = 0;                    返回(的MyType [])formatter.Deserialize(MS);
                }
      }

Okay, I have what I think is a simple question.. or just a case of me being a C# beginner.

I have an array of custom objects (clsScriptItem) that I am populating from a database. Once the items are loaded, I want to back them up to "backup" array so I can revert the information back after changing the main array. However, when I use CopyTo to copy the array and then alter the original array, the backup array is also being altered... I thought CopyTo merely copied values + structure from one array to another.

    private void backupItems()
    {
        //lastSavedItems and items are both of type clsScriptItem[] 
        //(declaration not shown)

        lastSavedItems = new clsScriptItem[items.Length];

        items.CopyTo(lastSavedItems, 0);

        //items[0].nexts[0] is 2
        //lastSavedItems[0].nexts[0] is 2

        items[0].nexts[0] = "-1";

        //items[0].nexts[0] is -1
        //lastSavedItems[0].nexts[0] is also -1


    }

How do I backup this data without having the two arrays be 'linked'??

UPDATE : I have updated the backup function to this

    private void backupItems()
    {
        lastSavedItems = new clsScriptItem[items.Length];
        for (int i = 0; i < items.Length; i++)
            lastSavedItems[i] = (clsScriptItem)items[i].Clone();

        items[0].nexts[0] = "-1";

    }

And I have update my class thusly....

public class clsScriptItem : ICloneable 
{

    //other declarations...

    object ICloneable.Clone() { return Clone(); }

    public clsScriptItem Clone()
    {
        return ((clsScriptItem)MemberwiseClone());
    }
}

** - and the same thing is happening.. Any thoughts?**

解决方案

try this:-

public static MyType[] DeepClone(MyType[] obj)
     {
                using (MemoryStream ms = new MemoryStream())
                {
                    BinaryFormatter formatter = new BinaryFormatter();
                    formatter.Serialize(ms, obj);
                    ms.Position = 0;

                    return (MyType[])formatter.Deserialize(ms);
                }
      }

这篇关于C#对象数组CopyTo用于连接两个数组的价值观?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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