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的更多相关文章

  1. 论文阅读(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 ...

  2. 【RS】Deep Learning based Recommender System: A Survey and New Perspectives - 基于深度学习的推荐系统:调查与新视角

    [论文标题]Deep Learning based Recommender System: A Survey and New Perspectives ( ACM Computing Surveys  ...

  3. 【原创】leetCodeOj --- Copy List with Random Pointer 解题报告

    题目地址: https://oj.leetcode.com/problems/copy-list-with-random-pointer/ 题目内容: A linked list is given s ...

  4. 【LeetCode】138. Copy List with Random Pointer

    题目: A linked list is given such that each node contains an additional random pointer which could poi ...

  5. 【Lintcode】105.Copy List with Random Pointer

    题目: A linked list is given such that each node contains an additional random pointer which could poi ...

  6. 【转载】Deep Learning(深度学习)学习笔记整理

    http://blog.csdn.net/zouxy09/article/details/8775360 一.概述 Artificial Intelligence,也就是人工智能,就像长生不老和星际漫 ...

  7. 【转】Deep Learning(深度学习)学习笔记整理系列之(八)

    十.总结与展望 1)Deep learning总结 深度学习是关于自动学习要建模的数据的潜在(隐含)分布的多层(复杂)表达的算法.换句话来说,深度学习算法自动的提取分类需要的低层次或者高层次特征. 高 ...

  8. 【转】 NSArray copy 问题

    转自:   http://blog.sina.com.cn/s/blog_6b1e4a060102uz0i.html   好久没写博客了,今天看到同事的代码中用到了 copy 这个 方法,之前也有了解 ...

  9. 【转】Deep Learning(深度学习)学习笔记整理系列之(六)

    9.3.Restricted Boltzmann Machine (RBM)限制波尔兹曼机 假设有一个二部图,每一层的节点之间没有链接,一层是可视层,即输入数据层(v),一层是隐藏层(h),如果假设所 ...

随机推荐

  1. js 监控浏览器关闭事件

    代码如下: <!DOCTYPE html> <html> <head> <title>监控浏览器关闭事件</title> </head ...

  2. linux 启动oracle报cannot restore segment prot after reloc: Permission denied

    error while loading shared libraries: $ORACLE_HOME/lib/libnnz10.so: cannot restore segment prot afte ...

  3. C#函数式程序设计之函数、委托和Lambda表达式

    C#函数式程序设计之函数.委托和Lambda表达式 C#函数式程序设计之函数.委托和Lambda表达式   相信很多人都听说过函数式编程,提到函数式程序设计,脑海里涌现出来更多的是Lisp.Haske ...

  4. NET 类库

    NET 类库研究必备参考 扣丁格鲁 .NET 类库的强大让我们很轻松的解决常见问题,作为一个好专研的程序员,为了更上一层楼,研究CLR的基础类库实现是快速稳定的捷径. 一般场景下,采用 Reflect ...

  5. 关于iTunes随机播放和我所不知道的自己

    无意中看到这套题,很有意思,自己做了一下. 规则是这样的:打开你的播放器,我的是iTunes,不管是哪个,总之打开最全的那个播放列表,开启随机播放,按顺序把每首歌名写在下面每道题的后面,比如第一首歌是 ...

  6. Identity

    ThreadStatic应用(Identity补完)   关于Identity Identity自增序列/唯一断标识 起初做这个东西,是在一个内部组件中,用于在高并发的环境下得到一个较短的“相对”不重 ...

  7. 推荐系列:最小与最大[DP+余式定理]

    最小与最大 [问题描述] 做过了乘积最大这道题,相信这道题也难不倒你. 已知一个数串,可以在适当的位置加入乘号(设加了k个,当然也可不加,即分成k+1个部分),设这k+1个部分的乘积(如果k=0,则乘 ...

  8. 百度云语音识别,Audio2Txt(c#)

    百度云识别没有提供c#版本的sdk,下面给个c#的 1.打开网址http://developer.baidu.com/ 2.登陆 3.管理控制台>开发者服务管理 4.创建工程 5.输入名称,点击 ...

  9. Django ORM 查询管理器

    Django ORM 查询管理器 ORM 查询管理器 对于 ORM 定义: 对象关系映射, Object Relational Mapping, ORM, 是一种程序设计技术,用于实现面向对象编程语言 ...

  10. Twitter算法

    算法实践——Twitter算法面试题(积水问题)的线性时间解法   问题描述:在下图里我们有不同高度的挡板.这个图片由一个整数数组所代表,数组中每个数是墙的高度.下图可以表示为数组(2.5.1.2.3 ...