You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
25 lines
748 B
25 lines
748 B
4 years ago
|
using System.Collections;
|
||
|
using System.Collections.Generic;
|
||
|
using System.IO;
|
||
|
using System.Runtime.Serialization;
|
||
|
using System.Runtime.Serialization.Formatters.Binary;
|
||
|
using UnityEngine;
|
||
|
|
||
|
/// <summary>
|
||
|
/// 利用反序列化深复制
|
||
|
/// </summary>
|
||
|
public class CloneBySerialize
|
||
|
{
|
||
|
|
||
|
public static T Clone<T>(T RealObject)
|
||
|
{
|
||
|
using (Stream objectStream = new MemoryStream())
|
||
|
{
|
||
|
//利用 System.Runtime.Serialization序列化与反序列化完成引用对象的复制
|
||
|
IFormatter formatter = new BinaryFormatter();
|
||
|
formatter.Serialize(objectStream, RealObject);
|
||
|
objectStream.Seek(0, SeekOrigin.Begin);
|
||
|
return (T)formatter.Deserialize(objectStream);
|
||
|
}
|
||
|
}
|
||
|
}
|