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