如下:

[Serializable]
public class ModelNewTable : ICloneable
{
    public object Clone()
    {
        using (var stream = new MemoryStream())
        {
            var formatter = new BinaryFormatter();
            formatter.Serialize(stream, this);
            stream.Seek(0, SeekOrigin.Begin);
            return formatter.Deserialize(stream);
        }
    }
}

 

class Program
{
    static void Main(string[] args)
    {
        var a = new Class1
            {
                a = 1,
                b = "abc",
                c = new StringBuilder("def")
            };
        var b = a.Clone();
        Console.Read();
    }
}
[Serializable]
public class Class1
{
    public int a;
    public string b;
    public StringBuilder c;

    public Class1 Clone()
    {
        using (var stream = new MemoryStream())
        {
            var formatter = new BinaryFormatter();
            formatter.Serialize(stream, this);
            stream.Seek(0, SeekOrigin.Begin);
            return (Class1)formatter.Deserialize(stream);
        }
    }
}

 

相关文章:

  • 2021-08-21
  • 2021-06-03
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-11
  • 2021-11-27
猜你喜欢
  • 2021-10-11
  • 2021-05-26
  • 2021-06-05
  • 2022-12-23
  • 2021-11-20
  • 2021-04-28
相关资源
相似解决方案