cloudflight
2024-06-10 4db7d08bb295be33e80f1353f58fcea4a8da6127
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;
 
namespace Hydro.CommonBase
{
    public static class DeepCopyClass
    {
        public static T DeepCopyByBin<T>(this T obj)
        {
            object 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 List<variable> DeepCopyByBinVariable(this List<variable> v)
        { 
            List<variable> retval = new List<variable>();
            foreach (var item in v)
            {
                retval.Add(item.Copy());
            }
            return retval;
        }
        public static void SerialToFile(this object obj,string filePath)
        {
            // 创建文件流
            FileStream fileStream = new FileStream(filePath, FileMode.Create);
 
            try
            {
                // 创建BinaryFormatter对象
                BinaryFormatter formatter = new BinaryFormatter();
 
                // 序列化并保存对象到文件流
                formatter.Serialize(fileStream, obj);
 
                Console.WriteLine("对象已经成功序列化并保存到文件中!");
            }
            catch (Exception ex)
            {
                Console.WriteLine("发生错误:" + ex.Message);
            }
            finally
            {
                // 关闭文件流
                fileStream.Close();
            }
 
        }
        private static T CloneModel<T>(T oModel)
        {
            var oRes = default(T);
            var oType = typeof(T);
 
            //create new obj
            oRes = (T)Activator.CreateInstance(oType);
 
            //pass 1 property
            var lstPro = oType.GetProperties();
            foreach (var oPro in lstPro)
            {
                var oValue = oPro.GetValue(oModel, null);
                oPro.SetValue(oRes, DeepCopy(oValue), null);
            }
 
            var lstField = oType.GetFields();
            foreach (var oField in lstField)
            {
                var oValue = oField.GetValue(oModel);
                oField.SetValue(oRes, DeepCopy(oValue));
            }
            return oRes;
        }
        public static object DeepCopy(this object obj)
        {
            if (obj == null)
                return null;
            Type type = obj.GetType();
 
            if (type.IsValueType || type == typeof(string))
            {
                return obj;
            }
            else if (type.IsArray)
            {
                Type elementType = Type.GetType(
                     type.FullName.Replace("[]", string.Empty));
                var array = obj as Array;
                Array copied = Array.CreateInstance(elementType, array.Length);
                for (int i = 0; i < array.Length; i++)
                {
                    copied.SetValue(DeepCopy(array.GetValue(i)), i);
                }
                return Convert.ChangeType(copied, obj.GetType());
            }
            else if (type.IsClass)
            {
 
                object toret = Activator.CreateInstance(obj.GetType());
                FieldInfo[] fields = type.GetFields(BindingFlags.Public |
                            BindingFlags.NonPublic | BindingFlags.Instance);
                foreach (FieldInfo field in fields)
                {
                    object fieldValue = field.GetValue(obj);
                    if (fieldValue == null)
                        continue;
                    field.SetValue(toret, DeepCopy(fieldValue));
                }
                return toret;
            }
            else
                throw new ArgumentException("Unknown type");
        }
    }
}