【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),如果假设所 ...
随机推荐
- .net的页面在大并发下出现503错误
.net的页面在大并发下偶尔出现503错误 我们开发了一个回调页面,由一个工具负责调用,由于压力非常大,回调页面通过6台服务器负载均衡的: 最近业务系统又再次扩容,回调页面压力成倍增加,在高峰时间段偶 ...
- ACdream 1195 Sudoku Checker (暴力)
Sudoku Checker Time Limit: 2000/1000MS (Java/Others)Memory Limit: 128000/64000KB (Java/Others) Submi ...
- 微信SDK使用总结
最近做一个分享功能,需要使用微信SDK分享内容给朋友或朋友圈.期间遇到些奇怪的问题,花了点时间去折腾了一番. 首先需要到微信开放平台http://open.weixin.qq.com/?lang=zh ...
- [问题]安装express,已经加了-g,还是找不到express命令
安装express时使用如下命令: npm install -g express 但是在命令行还是找不到express 手动将路径D:\Program Files (x86)\nodejs\node_ ...
- SignalR + KnockoutJS + ASP.NET MVC 实现井字游戏
SignalR + KnockoutJS + ASP.NET MVC 实现井字游戏 1.1.1 摘要 今天,我们将使用SignalR + KnockoutJS + ASP.NET MVC实现一个实 ...
- 创建一个js日历(原生JS实现日历)
前言 方法是有参考网上一些资料的,比如闰年的判断,比如每个月第一天是星期几的判断.不说太多,拆分出一个个函数,希望能描述尽可能的清晰明了. 一,判断闰年 //判断闰年 function runNian ...
- WinForm媒体播放器
媒体播放控件(Windows Media Player )的常用属性和方法,并且利用它设计一个简单的媒体应用程序——媒体播放器.该媒体播放器可以播放 wav.avi.mid 和 mp3 等格式的文件. ...
- Jumony Core 3,真正的HTML引擎
Jumony Core 3,真正的HTML引擎,正式版发布 2013-11-28 17:22 by Ivony..., 778 阅读, 18 评论, 收藏, 编辑 Jumony是一个开源项目,已经有三 ...
- .NET中文乱码解决方案
前言:最近升级一个由VS05开发的项目,当迁移至VS10后,试运行,啊~!我文盲了,怎么一个汉字都不认识了!(乱码纷纷的说) 说明:本文以将项目改为UTF8编码为例. 解决之道 1.修改配置文件 &l ...
- JS函数实现金额小写转大写
///<summery>小写金额转化大写金额</summery> function AmountLtoU(amount) { if (isNaN(amount) || amou ...