【C#】Deep copy of objects
If you learned C++ carefully, you must have known something about the copy of object.
For example, I defined a class called \(ABC\),and two variable \(x\) and \(y\) are \(ABC\)s.
Like this:
class ABC{
public:
int a,b;
};
int main(){
ABC x,y;
x.a=;x.b=;
y=x;
y.a=;
return ;
};
Certainly,\( y.a==3 , y.b==2 \), and, what's more, \( x.a==1,x.b==2 \).
So in fact,a deep copy was made in the line 8.
But in C#,this can be different, if you operate like this, \( x.a==3,x.b==2 \) would be the result, for x and y is the same variable.
So how can we make a deep copy? Do not worry,we can serialize it and then turn it back!
(Remember : Don't forget to put a \([Serializable]\) before the defination of the object!!!)
Enjoy it:(Line 31-35)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Runtime;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;
using System.Runtime.InteropServices; namespace DataTrans
{
public class Switcher
{
public byte[] Object2Bytes(object obj)
{
IFormatter fmt = new BinaryFormatter();
MemoryStream ms = new MemoryStream();
fmt.Serialize(ms, obj);
return ms.GetBuffer();
} public object Bytes2Object(byte[] bt)
{
IFormatter fmt = new BinaryFormatter();
MemoryStream ms = new MemoryStream(bt);
return (object)fmt.Deserialize(ms);
} public T DeepCopy<T>(T __dt)
{
byte[] bt = Object2Bytes(__dt); //serialize
return (T)Bytes2Object(bt); //deserialize
} public Switcher()
{
}
}
}
【C#】Deep copy of objects的更多相关文章
- 论文阅读(Lukas Neumann——【ICCV2017】Deep TextSpotter_An End-to-End Trainable Scene Text Localization and Recognition Framework)
Lukas Neumann——[ICCV2017]Deep TextSpotter_An End-to-End Trainable Scene Text Localization and Recogn ...
- 【RS】Deep Learning based Recommender System: A Survey and New Perspectives - 基于深度学习的推荐系统:调查与新视角
[论文标题]Deep Learning based Recommender System: A Survey and New Perspectives ( ACM Computing Surveys ...
- 【原创】leetCodeOj --- Copy List with Random Pointer 解题报告
题目地址: https://oj.leetcode.com/problems/copy-list-with-random-pointer/ 题目内容: A linked list is given s ...
- 【LeetCode】138. Copy List with Random Pointer
题目: A linked list is given such that each node contains an additional random pointer which could poi ...
- 【Lintcode】105.Copy List with Random Pointer
题目: A linked list is given such that each node contains an additional random pointer which could poi ...
- 【转载】Deep Learning(深度学习)学习笔记整理
http://blog.csdn.net/zouxy09/article/details/8775360 一.概述 Artificial Intelligence,也就是人工智能,就像长生不老和星际漫 ...
- 【转】Deep Learning(深度学习)学习笔记整理系列之(八)
十.总结与展望 1)Deep learning总结 深度学习是关于自动学习要建模的数据的潜在(隐含)分布的多层(复杂)表达的算法.换句话来说,深度学习算法自动的提取分类需要的低层次或者高层次特征. 高 ...
- 【转】 NSArray copy 问题
转自: http://blog.sina.com.cn/s/blog_6b1e4a060102uz0i.html 好久没写博客了,今天看到同事的代码中用到了 copy 这个 方法,之前也有了解 ...
- 【转】Deep Learning(深度学习)学习笔记整理系列之(六)
9.3.Restricted Boltzmann Machine (RBM)限制波尔兹曼机 假设有一个二部图,每一层的节点之间没有链接,一层是可视层,即输入数据层(v),一层是隐藏层(h),如果假设所 ...
随机推荐
- jquery-制作选项卡
强大的jquery-制作选项卡 最近在学习jquery,特地把今天写的一个选项卡源码贴出来.只是做只是梳理,大神们请不要吐槽,如果有更好的方法,欢迎指点.谢谢. css <style> ...
- SQL Server 后续去除功能汇总
原文:SQL Server 后续去除功能汇总 功能更新去除汇总 字段类型 在 Microsoft SQL Server 的未来版本中将删除 ntext.text 和 image 数据类型. 请避免在新 ...
- Android项目--浅析系统通讯录中的那些方法
系统通讯录,以前的版本虽然过时了,不过有些东西还是可以用. 1.开启系统联系人添加 /** 添加联系人 */ Intent intent = new Intent(Intent.ACTION_INSE ...
- 简话ASP.NET Web API
简话ASP.NET Web API 在vs2012中,我们很容易在根据选择的ASP.NET MVC Web应用程序来新建一个Web API应用,聪明的你一定想见得到,Web API和MVC有着某种联系 ...
- RMAN duplicate from active 时遭遇 ORA-17627 ORA-12154
最近在从活动数据库进行异机克隆时碰到了ORA-17629,ORA-17627,ORA-12154的错误,起初以为是一个Bug呢.Oracle Bug着实太多了,已经成了习惯性思维了.汗!错误提示是无法 ...
- DotNET应用架构设计指南 安全 运行管理和通讯策略
DotNET应用架构设计指南(第三章:安全 运行管理和通讯策略(13-16)) 安全 运行管理和通讯策略 组织策略定义的规则是支配应用程序如何安全,如何管理,不同的应用程序组件是如何和另一组件及外部服 ...
- API变了,客户端怎么办?
使用ASP.NET Web Api构建基于REST风格的服务实战系列教程[九]——API变了,客户端怎么办? 系列导航地址http://www.cnblogs.com/fzrain/p/3490137 ...
- 关于迭代器及yield的用法
1.foreach语句 C#编译器会把foreach语句转换为IEnumerable接口的方法和属性. foreach (Person p in persons) { Console.WriteLin ...
- Apache Rewrite 中文详解
这几天一直在研究Apache的重写规则,虽然网上有很多教程,不过发现大部分都是抄袭一个人的,一点都不全,所以我想写一个简单的易于理解的教程,我学习.htaccess是从目录保护开始的,这个也比较简单, ...
- vs中web配置可浏览json数据文件
在日常的前端开发中,我们会制作一些数据文件,常用的有后缀名为json的文件,但是vs在启动项目时,是不能浏览json文件的,常见的错误提示如下图所示 解决这个错误,只需要在web.config文件中配 ...