C# 扩展方法克隆实体类

using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;
using System.Collections.Concurrent;
using System.Collections.Generic;

namespace study
{
    public class Program
    {
        static void Main(string[] args)
        {
            Show s = new Show();
            s.ShadowCopy();
        }

    public class Show
    {
        public void ShadowCopy()
        {
            TestData testData = new TestData { Name = "A", Count = 1 };
            TestData cloneTestData = testData.CloneEntity();
            bool same = testData.Equals(cloneTestData);
            int i=testData.Name.WordCount();
            Console.WriteLine(same);
            Console.WriteLine(i);
            testData.Name.Print();
           
            Console.ReadKey();
        }

        public void DeepCopy()
        {
            Entity entity = new Entity { Name = "Bruce", Line = 1 };
            Entity cloneEntity = entity.Clone();
            bool same = entity.Equals(cloneEntity);
            Console.WriteLine(same);
            List<Entity> list = new List<Entity>();
            list.Add(new Entity { Name = "Bruce", Line = 1 });
            list.Add(new Entity { Name = "Jack", Line = 2 });
            list.Add(new Entity { Name = "Rose", Line = 3 });
            list.Add(new Entity { Name = "Tony", Line = 4 });
            List<Entity> cloneList = list.Clone();
            foreach (var item in cloneList)
            {
                Console.WriteLine(item.Name+" "+item.Line);
            }
            
            Console.ReadKey();
        }
    }

    #region 共通复制实体类的方法测试
    public class TestData : TestBase
    {
        public string Name { get; set; }
        public int Count { get; set; }
    }

    public class TestBase
    {
        public object CloneObject()
        {
            return this.MemberwiseClone();
        }
    }
    /// <summary>
    /// 1、定义一个静态类以包含扩展方法。该类必须对客户端代码可见。 
    /// 2、将该扩展方法实现为静态方法,并使其至少具有与包含类相同的可见性。
    /// 3、该方法的第一个参数指定方法所操作的类型;该参数必须以 this 修饰符开头。
    /// 4、在调用代码中,添加一条 using 指令以指定包含扩展方法类的命名空间。
    /// 5、按照与调用类型上的实例方法一样的方式调用扩展方法。
    /// </summary>
    public static class TestClone
    {
        public static T CloneEntity<T>(this T org) where T : TestBase
        {
            return (T)org.CloneObject();
        }
        public static int WordCount(this String str)
        {
            return str.Split(new char[] { ' ', '.', '?' }, StringSplitOptions.RemoveEmptyEntries).Length;
        }
        public static void Print(this String str)
        {
            Console.WriteLine(str);
        }
    }
    #endregion

    public static class DeepClone
    {
        public static T Clone<T>(this T t)
        {
            return (T)CloneObject(t);
        }

        private static object CloneObject(object obj)
        {
            using (MemoryStream memStream = new MemoryStream())
            {
                BinaryFormatter binaryFormatter = new BinaryFormatter(null, new StreamingContext(StreamingContextStates.Clone));
                binaryFormatter.Serialize(memStream, obj);
                memStream.Seek(0, SeekOrigin.Begin);
                return binaryFormatter.Deserialize(memStream);
            }
        }
    }

    /// <summary>
    /// 被克隆的类必须标记为可序列化
    /// </summary>
    [Serializable]
    public class Entity 
    { 
        public string Name { get; set; } 
        public int Line { get; set; } 
    }
}

  

原文地址:https://www.cnblogs.com/akatuki/p/3696446.html