浏览代码

深度复制

胡佳骏 1 年之前
父节点
当前提交
31f8b5bcfb
共有 2 个文件被更改,包括 40 次插入0 次删除
  1. 29 0
      Assets/2.0/Clone.cs
  2. 11 0
      Assets/2.0/Clone.cs.meta

+ 29 - 0
Assets/2.0/Clone.cs

@@ -0,0 +1,29 @@
+using System;
+using System.IO;
+using System.Runtime.Serialization;
+using System.Runtime.Serialization.Formatters.Binary;
+
+public class Clone
+{
+
+    public static T DeepCopy<T>(T source)
+    {
+
+        if (!typeof(T).IsSerializable)
+        {
+            throw new ArgumentException("The type must be serializable.", "source");
+        }
+        if (Object.ReferenceEquals(source, null))
+        {
+            return default(T);
+        }
+        IFormatter formatter = new BinaryFormatter();
+        Stream stream = new MemoryStream();
+        using (stream)
+        {
+            formatter.Serialize(stream, source);
+            stream.Seek(0, SeekOrigin.Begin);
+            return (T)formatter.Deserialize(stream);
+        }
+    }
+}

+ 11 - 0
Assets/2.0/Clone.cs.meta

@@ -0,0 +1,11 @@
+fileFormatVersion: 2
+guid: 8e2f2df7a03f54c45a18e41b0a7458c5
+MonoImporter:
+  externalObjects: {}
+  serializedVersion: 2
+  defaultReferences: []
+  executionOrder: 0
+  icon: {instanceID: 0}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: