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

  

C# 扩展方法克隆实体类的更多相关文章

  1. .NET Core中间件的注册和管道的构建(2)---- 用UseMiddleware扩展方法注册中间件类

    .NET Core中间件的注册和管道的构建(2)---- 用UseMiddleware扩展方法注册中间件类 0x00 为什么要引入扩展方法 有的中间件功能比较简单,有的则比较复杂,并且依赖其它组件.除 ...

  2. 用UseMiddleware扩展方法注册中间件类

    用UseMiddleware扩展方法注册中间件类 .NET Core中间件的注册和管道的构建(2)---- 用UseMiddleware扩展方法注册中间件类 0x00 为什么要引入扩展方法 有的中间件 ...

  3. 提升开发效率的notepad++一些快捷方法(实体类的创建和查询sql语句的编写)

    新手要创建数据库表中,对应字段名的实体类,是不是感觉很麻烦,可以用notepad++快速的把实体类中的字段名进行排版,随后直接粘入idea使用 下面是navicat的演示 选择一个表,右键选择设计表 ...

  4. .net core 2使用ef core 2.0以db first方法创建实体类

    先安装以下三个包: Install-Package Microsoft.EntityFrameworkCore.SqlServer Install-Package Microsoft.EntityFr ...

  5. mybatis主键返回语句 使用方法,就是实体类.getid即可拿到返回的id

    <insert id="insertSelective" parameterType="com.o2o.Content" useGeneratedKeys ...

  6. .NET手记-定义类和接口的扩展方法

    对于iOS开发者来说,使用扩展方法是家常便饭.因为有很多的类是有系统框架的定义的,我们不能修改或者不想修改他们的源码,但是我们又想要给他添加一些扩展方法来使用.这时定义扩展方法就是很有用的方式了,正如 ...

  7. 【springboot】【idea】实体类免写get、set等方法,使用lombok依赖和插件的@Data类注解

    需求,一个实体类,规范写法一定要对应的get.set方法,有必要还要重写toString方法.虽然可以快速生成get.set等方法,但是如果要添加或减少成员属性时就得重新生成get.set等方法. 而 ...

  8. EF里查看/修改实体的当前值、原始值和数据库值以及重写SaveChanges方法记录实体状态

    本文目录 查看实体当前.原始和数据库值:DbEntityEntry 查看实体的某个属性值:GetValue<TValue>方法 拷贝DbPropertyValues到实体:ToObject ...

  9. C#扩展方法知多少

    前言:上篇 序列化效率比拼——谁是最后的赢家Newtonsoft.Json 介绍了下序列化方面的知识.看过Demo的朋友可能注意到了里面就用到过泛型的扩展方法,本篇打算总结下C#扩展方法的用法.博主打 ...

随机推荐

  1. [转载]C++ 堆与栈简单的介绍

    在C和C++中,有三种使用存储区的基本方式: [静态存储区(Static   Memory)] 在静态存储区中,连接器(linker)根据程序的需求为对象分配空间.全局变量.静态类成员以及函数中的静态 ...

  2. 你好,C++(6)2.3 C++兵器谱

    2.3  C++兵器谱 正所谓“工欲善其事,必先利其器”,而要想做好C++程序设计,自然也离不开几件像样的兵器.下面我们就来看看C++兵器谱上有哪些神兵利器值得我们学习掌握.排在兵器谱上首要位置的就是 ...

  3. 对于js原型和原型链继承的简单理解(第二种,对象冒充)

    关键代码    this.parent = Cat;    this.parent.apply(this); function Cat(){ this.climb = function(){ aler ...

  4. 基类方法的反隐藏 反private 秘籍

    class GoodStudent:private Mentor,private Student { public : using Mentor::GetInfo;   ///------------ ...

  5. 字符串时间日期转为Date格式和long格式

    public static Long compare_date(String DATE1, String DATE2) { DateFormat df = new SimpleDateFormat(& ...

  6. Sogou搜狗搜索引擎登录网站 - Blog透视镜

    Sogou搜狗是中国搜狐旗下的搜索引擎,其登录方式也很简单,只要输入网址,验证码即可,不需要注册账号,再进行登录,其他非 * 必填的字段,可留下空白不填,验证码只有4码,也很清晰可见,不像有的网站,即 ...

  7. Activity 模版样式简介

    1:对话框样式. <activity android:theme="@android:style/Theme.Dialog"> 2:透明样式. <activity ...

  8. KEIL中的变量相关

    例:sfr P0=0x80表示P0口地址为80H:(sfr是字节操作) sfr16 T2=0xCC表示T2口地址的低地址为TL2=0XCC,高地址为TH2=0XCD(sfr16是字操作) 头文件reg ...

  9. c语言typedef运用与函数指针

    #include <stdio.h> #include <stdlib.h> #define PINT int * typedef short* PSHORT; //typed ...

  10. day55

    担心了好久的编译原理也总是考完了 大学里的最后一次考试也是结束罗 这次的考试起伏跌宕啊 我们本来是9点钟开始考试 但是我们班的几个同学基本上7点钟就去了 为了什么?? 选个好的位置撒哈哈,到了九点,老 ...