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

  

最新文章

  1. Mybatis传入参数类型为Map
  2. 老生长谈的$.extend()方法
  3. MySQL选择合适的数据类型
  4. HTML5开发笔记:初窥CANVAS,上传canvas图片到服务器
  5. C# ArrayList的用法
  6. Html登录表单阻止自动填充
  7. PureBasic—数控编辑框与调节块和进度条
  8. 灰色预测模型 c# 算法实现
  9. nodejs笔记五--MongoDB基本环境配置及增删改查;
  10. JQuery 预热
  11. python challenge 16
  12. openstack安装记录(二)keystone安装
  13. 基于visual Studio2013解决C语言竞赛题之0808打印链表
  14. js string 转 int 注意的问题——parseInt
  15. squid安装及运行指南
  16. 关于myeclipse启动报错:An internal error has occurred. java.lang.NullPointerException解决办法
  17. webpack vue-loader was used without the corresponding plugin. Make sure to include VueLoaderPlugin
  18. Maven项目在更新过程停止,再更新无效--&gt;解决
  19. centos7 快速安装 mariadb(mysql)
  20. 聊聊Java happens-before原则

热门文章

  1. PHP 计算页面执行时间
  2. IIS下 Yii Url重写
  3. IE6 png 透明 (三种解决方法)(转来的哦)
  4. Android网络框架比较
  5. laravel实现第三方登录(qq登录)
  6. Unity中Mecanim工作流
  7. js深入研究之牛逼的类封装设计
  8. C++ int 转换成 string intToString
  9. About Adultism and why things ar the way they are
  10. 关于bootstrap--表单(按钮&lt;button&gt;效果、大小、禁用)