cloudflight
2024-06-10 07ffb6029da759a8ce4498207cfa5f6d069c44b1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
 
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;
 
namespace CloudWaterNetwork
{
    public static class copy
    {
       
        public static List<dynamic> deepcopy(List<object> obj)
        {
            return DeepCopyByBin<List<dynamic>>(obj);
        }
     
        public static List<string> deepcopy(List<string> obj)
        {
            return DeepCopyByBin<List<string>>(obj);
        }
 
        public static T DeepCopyByBin<T>(T obj)
        {
            dynamic retval;
            using (MemoryStream ms = new MemoryStream())
            {
                BinaryFormatter bf = new BinaryFormatter();
                //序列化成流
                bf.Serialize(ms, obj);
                ms.Seek(0, SeekOrigin.Begin);
                //反序列化成对象
                retval = bf.Deserialize(ms);
                ms.Close();
            }
            return (T)retval;
        }
 
 
        public static T DeepCopy<T>(this T obj)
        {
            dynamic retval;
            using (MemoryStream ms = new MemoryStream())
            {
                BinaryFormatter bf = new BinaryFormatter();
                //序列化成流
                bf.Serialize(ms, obj);
                ms.Seek(0, SeekOrigin.Begin);
                //反序列化成对象
                retval = bf.Deserialize(ms);
                ms.Close();
            }
            return (T)retval;
        }
 
 
    }
 
 
}