【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),如果假设所 ...
随机推荐
- Json.Net6.0
Json.Net6.0入门学习试水篇 前言 JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.简单地说,JSON 可以将 JavaScript 对象中表 ...
- Visual Studio 2013 上使用 Github
教你如何在 Visual Studio 2013 上使用 Github 介绍 我承认越是能将事情变简单的工具我越会更多地使用它.尽管我已经知道了足够的命令来使用Github,但我宁愿它被集成到IDE中 ...
- Ninject依赖注入——构造函数、属性、方法和字段的注入
Ninject依赖注入——构造函数.属性.方法和字段的注入(三) 1.Ninject简介 Ninject是基于.Net平台的依赖注入框架,它能够将应用程序分离成一个个高内聚.低耦合(loosely-c ...
- web 富文本编辑器总结
前言 富文本编辑器,就是除了能输入不同的文本之外,还可以之间粘贴图画等其他的多媒体信息.也可说是所见即所得的编辑器. 目前可以使用的编辑器有很多, 在网络上有找到这样一份比较表格: 编辑器 产地 稳定 ...
- Binder机制,从Java到C (6. Binder in Native : libbinder)
1.Java和C++中的Binder 从前一篇 Binder机制,从Java到C (5. IBinder对象传递形式) 中可以看到,使用Binder的Java代码,到最后都会进入到Native环境,将 ...
- Dynamics CRM不发布JS调试
本博客已迁移至 http://www.krely.cn/ 上个项目做了一年多,大多是在做JS的开发,由于开发人数比较多,着实被坑的不轻.因为JS修改完成之后必须要发布,对于我们动辄几千行的JS的调 ...
- 1 MySQL概述
目录: 1. 简述 2. 历史 3. 同类产品 4. 优点和不足 5. MySQL存储引擎 6. MySQL架构 1. 简述 MySQL是一个关系型数据库管理系统.其体积小,速度快,开发源代码,使用成 ...
- 【转】几点 iOS 开发技巧
[译] 几点 iOS 开发技巧 原文:iOS Programming Architecture and Design Guidelines 原文来自破船的分享 原文作者是开发界中知晓度相当高的 Mug ...
- jquery数据验证插件
jquery数据验证插件(自制,简单,练手) 一:最近项目中js数据验证比较多,为了统一风格,移植复用,于是顺手封装了Jquery的插件. (function($) { var defaults ...
- sql 清除日志空间
USE DBCenter GO SELECT file_id, name FROM sys.database_files; 查找日志名称 USE DBCenter ; GO ALTER DATABAS ...