话谈c#拷贝
c#中类型分为值类型和引用类型,值类型对象赋值是本身就是赋的自身的一个副本,而引用类型赋值时则赋的是指向堆上的内存,假如我们不想赋这个地址而想将对象赋过去要怎么做呢?首先要知道拷贝分为浅表拷贝和深层拷贝,浅表拷贝得到一个新的实例,一个与原始对象类型相同、值类型字段相同的拷贝。但是,如果字段是引用类型的,则拷贝的是该引用, 而不是的对象。若想将引用字段的对象也拷贝过去,则称为深拷贝。 为了实现拷贝,本文总结了以下几种方法。
1.首先就是最笨的方法,传说中的“人工拷贝”,就是将引用里的所有值对象和具有值特征的string对象一一赋给新对象,这种方式代码量过大而且维护起来相当麻烦,所以能不用就不用。
2.System.Object提供了受保护的方法 MemberwiseClone,可用来实现“浅表”拷贝。由于该方法标记为“受保护”级别,因此,我们只能在继承类或该类内部才能访问该方法。
public class A
{
public string rr { get; set; }
public string tt { get; set; } public A ShallowCopy()
{
return (A)this.MemberwiseClone();
} }
3.使用序列化与反序列化的方式,这种方式虽可实现深度拷贝,但有点大炮打蚊子的味道,而且在外面引用时一定要记得关闭所创建的MemoryStream流
public static object Clone(object o,out MemoryStream ms)
{
BinaryFormatter bf = new BinaryFormatter(); ms = new MemoryStream();
bf.Serialize(ms,o);
ms.Seek(, SeekOrigin.Begin); return bf.Deserialize(ms);
}
4.在一个外国人写的博客中(http://www.codeproject.com/Articles/3441/Base-class-for-cloning-an-object-in-C),使用反射的方法来解决了这个问题。他写了一个BaseObject类,如果我们继承这个类就可以实现深度拷贝,下面是他的实现方法:
创建一个实现 ICloneable
接口的有默认行为的抽象类,所谓的默认行为就是使用以下库函数来拷贝类里的每一个字段。
1.遍历类里的每个字段,看看是否支持ICloneable
接口。
2.如果不支持ICloneable
接口,按下面规则进行设置,也就是说如果字段是个值类型,那将其拷贝,如果是引用类型则拷贝字段指向通一个对象。
3.如果支持ICloneable
,在科隆对象中使用它的克隆方法进行设置。
4.如果字段支持IEnumerable接口,需要看看它是否支持IList或者IDirectionary接口,如果支持,迭代集合看看是否支持ICloneable接口。
我们所要做的就是使得所有字段支持ICloneable接口。
下面是测试结果:
public class MyClass : BaseObject
{
public string myStr =”test”;
public int id;
} public class MyContainer : BaseObject
{
public string name = “test2”;
public MyClass[] myArray= new MyClass[]; public class MyContainer()
{
for(int i= ; i< ; i++)
{
this.myArray[I] = new MyClass();
}
}
}
static void Main(string[] args)
{
MyContainer con1 = new MyContainer();
MyContainer con2 = (MyContainer)con1.Clone(); con2.myArray[].id = ;
}
con2中MyClass实例中第一个元素变成了5,但con1没有改变,即实现了深拷贝。
BaseObject的实现:
/// <summary>
/// BaseObject class is an abstract class for you to derive from.
/// Every class that will be dirived from this class will support the
/// Clone method automaticly.<br>
/// The class implements the interface ICloneable and there
/// for every object that will be derived <br>
/// from this object will support the ICloneable interface as well.
/// </summary> public abstract class BaseObject : ICloneable
{
/// <summary>
/// Clone the object, and returning a reference to a cloned object.
/// </summary>
/// <returns>Reference to the new cloned
/// object.</returns>
public object Clone()
{
//First we create an instance of this specific type.
object newObject = Activator.CreateInstance( this.GetType() ); //We get the array of fields for the new type instance.
FieldInfo[] fields = newObject.GetType().GetFields(); int i = ; foreach( FieldInfo fi in this.GetType().GetFields() )
{
//We query if the fiels support the ICloneable interface.
Type ICloneType = fi.FieldType.
GetInterface( "ICloneable" , true ); if( ICloneType != null )
{
//Getting the ICloneable interface from the object.
ICloneable IClone = (ICloneable)fi.GetValue(this); //We use the clone method to set the new value to the field.
fields[i].SetValue( newObject , IClone.Clone() );
}
else
{
// If the field doesn't support the ICloneable
// interface then just set it.
fields[i].SetValue( newObject , fi.GetValue(this) );
} //Now we check if the object support the
//IEnumerable interface, so if it does
//we need to enumerate all its items and check if
//they support the ICloneable interface.
Type IEnumerableType = fi.FieldType.GetInterface
( "IEnumerable" , true );
if( IEnumerableType != null )
{
//Get the IEnumerable interface from the field.
IEnumerable IEnum = (IEnumerable)fi.GetValue(this); //This version support the IList and the
//IDictionary interfaces to iterate on collections.
Type IListType = fields[i].FieldType.GetInterface
( "IList" , true );
Type IDicType = fields[i].FieldType.GetInterface
( "IDictionary" , true ); int j = ;
if( IListType != null )
{
//Getting the IList interface.
IList list = (IList)fields[i].GetValue(newObject); foreach( object obj in IEnum )
{
//Checking to see if the current item
//support the ICloneable interface.
ICloneType = obj.GetType().
GetInterface( "ICloneable" , true ); if( ICloneType != null )
{
//If it does support the ICloneable interface,
//we use it to set the clone of
//the object in the list.
ICloneable clone = (ICloneable)obj; list[j] = clone.Clone();
} //NOTE: If the item in the list is not
//support the ICloneable interface then in the
//cloned list this item will be the same
//item as in the original list
//(as long as this type is a reference type). j++;
}
}
else if( IDicType != null )
{
//Getting the dictionary interface.
IDictionary dic = (IDictionary)fields[i].
GetValue(newObject);
j = ; foreach( DictionaryEntry de in IEnum )
{
//Checking to see if the item
//support the ICloneable interface.
ICloneType = de.Value.GetType().
GetInterface( "ICloneable" , true ); if( ICloneType != null )
{
ICloneable clone = (ICloneable)de.Value; dic[de.Key] = clone.Clone();
}
j++;
}
}
}
i++;
}
return newObject;
}
}
话谈c#拷贝的更多相关文章
- Python之浅谈深浅拷贝
目录 深浅拷贝 拷贝 浅拷贝 深拷贝 深浅拷贝 拷贝 s=['tim','age'] s2=s #这里的s2列表指向与s相同的id 当s2为s的拷贝对象时,s内的可变类型变化,s2变化;s内的不可变类 ...
- 话谈C#第二天
今天做了几个小小的练习,和大家分享一下. 1.用*打印出等腰三角形,代码如下: static void Main(string[] args) { int n = 5; for (int i = 1; ...
- 话谈C#第一天
今天是第一次接触C#,由于长时间的做Java开发,突然转到C#非常的不自然,但是也有了一些收获,给大家分享一下 using System; using System.Collections.Gener ...
- c#拷贝
话谈c#拷贝 c#中类型分为值类型和引用类型,值类型对象赋值是本身就是赋的自身的一个副本,而引用类型赋值时则赋的是指向堆上的内存,假如我们不想赋这个地址而想将对象赋过去要怎么做呢?首先要知道拷贝分为浅 ...
- 从追MM谈Java的23种设计模式(转)
从追MM谈Java的23种设计模式 这个是从某个文章转载过来的.但是忘了原文链接.如果知道的,我追加一下. 1.FACTORY-追MM少不了请吃饭了,麦当劳的鸡翅和肯德基的鸡翅都是MM爱吃的东西 ...
- 从追MM谈Java的23种设计模式
从追MM谈Java的23种设计模式 1.FACTORY—追MM少不了请吃饭了,麦当劳的鸡翅和肯德基的鸡翅都是MM爱吃的东西,虽然口味有所不同,但不管你带MM去麦当劳或肯 德基,只管向服务员说“来四个鸡 ...
- 【转】经典网文:追MM与设计模式
设计模式做为程序员的“内功心法”,越来越受到.net 社区的重视,这种变化是很可喜的,Java社区走在了我们的前面,但这种状况也许有一天会发生改变. 从追MM谈Java的23种设计模式1.FACT ...
- [SL] Silverlight + WCF Demo项目
I:项目描述:利用 Silverlight+WCF 技术,模拟资源管理器(如图1)功能,通过地址栏输入本地文件夹路径,然后将解析出来的该目录下所有文件(夹)存储到数据库中,然后再加载到界面上显示出来: ...
- fir.im Weekly - 一切从知识重构开始
一年之计在于春,大自然开始了新元素的重构.你的知识库是否也该重构更新呢? 本期 fir.im Weekly 包含最新的Android.iOS 开发工具.源码和好玩的UI 动画分享,希望对你有用. Sw ...
随机推荐
- 对Unity3d C#手动处理异常产生
System.AppDomain.CurrentDomain.UnhandledException += new System.UnhandledExceptionEventHandler(_OnUn ...
- Oracle安装及使用入门
新手Oracle安装及使用入门 一.安装Oracle Step1 下载oracle压缩包并解压到同一文件夹下面 Step2 双击setup.exe进行安装 Step3:进入如下界面配置: 邮箱可不 ...
- django csrf_token生成
django模板中生成csrf_token的不同方式 系统环境 CENTOS 6.4 python 2.7.6 django 1.7.1 当post提交表单的的时候,是需要 csrf_token的, ...
- hdu 5073 Galaxy(2014acm鞍山亚洲分部 D)
主题链接:http://acm.hdu.edu.cn/showproblem.php? pid=5073 Galaxy Time Limit: 2000/1000 MS (Java/Others) ...
- iOS MapKit导航及地理转码辅助类
头文件: #import <Foundation/Foundation.h> #import <MapKit/MapKit.h> @interface DirectionRou ...
- 欧几里德欧几里德原理和扩展的原则,(Euclidean Theory and Extended Euclidean Theory)学习笔记
题记:这是我第四次审查扩展欧几里德原理,由于不经常使用.当你想使用,可以不记得细节,经常检查信息,所以,简单地梳理这一原则和扩展欧几里德的原则,以博客存档以备查用. 一个.欧几里德原理 欧几里德原理( ...
- ORACLE 中极易混淆的几个 NAME 的分析和总结
我们知道,Oracle中的各种NAME会在我们的各个配置文件里常常出现,大致有下面这些: 在init.ora中有DB_NAME,INSTANCE_NAME,SERVICE_NAME 配置DG的时候,为 ...
- ShellExecute函数简单说明
平时在delphi写代码的过程中总是能遇到ShellExecute函数,于是索性将它的使用方法整理一下,由于我在微软的站点上也没能查到个详解(当然我查的中文版,俺菜嘛) ShellExecute函数原 ...
- SQL点滴29—错误无处不在
原文:SQL点滴29-错误无处不在 我只想说以下是很基础的sql知识,但是很容易犯错.所以睁大我们的眼睛,屏住我们的呼吸,小心的检查吧! 案例1if not exists (select OrderI ...
- linuxsocket通信recv研究缓存机制
曾有过这样一个小疑问.当一个进程注册的插座后,,假设插座没有被调用recv函数接受包.能接受到数据包吗? 或者这样说,假设我的程序注冊了一个套接字去接受数据包,可是每收到一个数据包都须要非常长 ...