Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/261.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
复制列表对象C#_C# - Fatal编程技术网

复制列表对象C#

复制列表对象C#,c#,C#,我有两个列表对象,我试图将一个列表的值复制到另一个列表。理想的编码是: List x=新列表(); 列表温度=新列表(); 列表y=新列表(); int myCount; foreach(列表t在y中) { myCount=t.计数; 对于(int w=0;wpublic static T DeepClone(T obj) { 使用(var ms=new MemoryStream()) { var formatter=新的二进制格式化程序(); 序列化(ms,obj); ms.Position

我有两个列表对象,我试图将一个列表的值复制到另一个列表。理想的编码是:

List x=新列表();
列表温度=新列表();
列表y=新列表();
int myCount;
foreach(列表t在y中)
{
myCount=t.计数;
对于(int w=0;w
public static T DeepClone(T obj)
{
使用(var ms=new MemoryStream())
{
var formatter=新的二进制格式化程序();
序列化(ms,obj);
ms.Position=0;
返回(T)格式化程序。反序列化(ms);
}
}
温度=深度克隆(t);
公共静态T深度克隆(T obj)
{
使用(var ms=new MemoryStream())
{
var formatter=新的二进制格式化程序();
序列化(ms,obj);
ms.Position=0;
返回(T)格式化程序。反序列化(ms);
}
}
温度=深度克隆(t);

你是对的-找到了其他让我失败的线程。那个线程帮助了我。谢谢。你是对的-找到了其他让我失败的线程。那个线程帮助了我。谢谢。
 public static T DeepClone<T>(T obj)
    {
        using (var ms = new MemoryStream())
        {
            var formatter = new BinaryFormatter();
            formatter.Serialize(ms, obj);
            ms.Position = 0;

            return (T)formatter.Deserialize(ms);
        }
    }
temp = DeepClone(t);