zoukankan
html css js c++ java
实现深拷贝的一种方法
声明接口:
public
interface
IClone
{
T Clone
<
T
>
(T instance)
where
T :
class
;
}
建立CloneManager类实现接口:
public
class
CloneManager:IClone
{
/**/
///
<summary>
///
Clones the specified instance.
///
</summary>
///
<typeparam name="T"></typeparam>
///
<param name="instance">
The instance.
</param>
///
<returns>
A new instance of an object.
</returns>
T IClone.Clone
<
T
>
(T instance)
{
XmlSerializer serializer
=
new
XmlSerializer(
typeof
(T));
MemoryStream stream
=
new
MemoryStream();
serializer.Serialize(stream, instance);
stream.Seek(
0
, SeekOrigin.Begin);
return
serializer.Deserialize(stream)
as
T;
}
}
实际应用代码:
class
Program
{
static
void
Main(
string
[] args)
{
IClone clone
=
new
CloneManager.CloneManager();
AA ta
=
new
AA();
ta.aa
=
123
;
ta.bb
=
234
;
AA tb
=
clone.Clone
<
AA
>
(ta);
ta.aa
=
999
;
ta.bb
=
888
;
System.Console.WriteLine(ta.aa);
System.Console.WriteLine(ta.bb);
System.Console.WriteLine(tb.aa);
System.Console.WriteLine(tb.bb);
}
}
public
class
AA
{
public
int
aa
=
0
;
public
int
bb
=
0
;
}
简单吧!!
查看全文
相关阅读:
centos8重置root密码
Mysql查询某列最长字符串记录
ssm连接mysql出现Connections could not be acquired from the underlying database 问题
spring-基于xml配置Bean
WinForm控件命名缩写
SQL Server 将一张表的某个字段更新到另一张表中
SQL Server 取出指定字符后字符串(用于分割)
小白学CMD下运行MySQL
Bootstrap3.0和bootstrap2.x的区别
有关js弹出提示框几种方法
原文地址:https://www.cnblogs.com/adam/p/1165282.html
最新文章
Android多媒体框架
apk混淆打包和反编译(转)
vim命令(转)
安卓中运行报错Error:Execution failed for task ':app:transformClassesWithDexForDebug'解决
web应用中web.xml文件的解释
在网上找到的一些Java封装的utils类
java 嵌入式数据库H2
文件上传下载
Spring MVC 模拟
监听器的应用
热门文章
filter 应用
监听器
filter 开发
filter
Apache的DBUtils框架学习(转)
数据库连接池(转)
编写自己的JDBC框架(转)
Springboot访问不了static文件夹的静态资源,配置拦截器出现“No mapping for GET“静态资源的情况
linux 安装java8
centos8 网络配置
Copyright © 2011-2022 走看看