• 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; } 
        }
    }
    

      

  • 相关阅读:
    EF中,保存实体报错:Validation failed for one or more entities. 如何知道具体错误在哪?
    个人常用的移动端浅灰底index.html
    分享一段js,判断是否在iPhone中的Safari浏览器打开的页面
    2017年3月25日,祝自己23岁生日快乐!
    JavaScript监控页面input输入整数且只能输入2位小数
    iPhone IOS10安装APP没提示连接网络(无法联网)的解决办法
    iPhone屏蔽IOS更新、iPhone系统更新的提示(免越狱,有效期更新至2021年)
    iPhone Anywehre虚拟定位提示“后台服务未启动,请重新安装应用后使用”的解决方法
    C# OracleDBhelper
    戴尔灵越15-5000/3558等系列修改BIOS设置U盘启动
  • 原文地址:https://www.cnblogs.com/akatuki/p/3696446.html
Copyright © 2020-2023  润新知