zoukankan      html  css  js  c++  java
  • C#原型模式

    如下:

    [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);
            }
        }
    }
  • 相关阅读:
    Catalan数
    完全背包
    日期问题
    01背包
    NOJ2076
    858. Prim算法求最小生成树
    839. 模拟堆
    850. Dijkstra求最短路 II
    849. Dijkstra求最短路 I
    859. Kruskal算法求最小生成树
  • 原文地址:https://www.cnblogs.com/yao2yao4/p/3236654.html
Copyright © 2011-2022 走看看