{
MemoryStream ms = new MemoryStream();
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(ms, this );
ms.Position = 0 ;
return bf.Deserialize(ms) as AtmDataBase;
}
值类型(Value Type)(如 char、int 和 float)、枚举类型和结构类型。
引用类型(Reference Type) 包括类 (Class) 类型、接口类型、委托类型和数组类型。
如何来划分它们?
以它们在计算机内存中如何分配来划分
值类型与引用类型的区别?
1,值类型的变量直接包含其数据,
2,引用类型的变量则存储对象引用。
对于引用类型,两个变量可能引用同一个对象,因此对一个变量的操作可能影响另一个变量所引用的对象。对于值类型,每个变量都有自己的数据副本,对一个变量的操作不可能影响另一个变量。
值类型隐式继承自System.ValueType 所以不能显示让一个结构继承一个类,C#不支持多继承
堆栈(stack)是一种先进先出的数据结构,在内存中,变量会被分配在堆栈上来进行操作。
堆(heap)是用于为类型实例(对象)分配空间的内存区域,在堆上创建一个对象,
会将对象的地址传给堆栈上的变量(反过来叫变量指向此对象,或者变量引用此对象)。
关于对象克隆的所设计到知识点
浅拷贝:是指将对象中的所有字段逐字复杂到一个新对象
对值类型字段只是简单的拷贝一个副本到目标对象,改变目标对象中值类型字段的值不会反映到原始对象中,因为拷贝的是副本
对引用型字段则是指拷贝他的一个引用到目标对象。改变目标对象中引用类型字段的值它将反映到原始对象中,因为拷贝的是指向堆是上的一个地址
深拷贝:深拷贝与浅拷贝不同的是对于引用字段的处理,深拷贝将会在新对象中创建一个新的对象和
原始对象中对应字段相同(内容相同)的字段,也就是说这个引用和原始对象的引用是不同, 我们改变新
对象中这个字段的时候是不会影响到原始对象中对应字段的内容。
浅复制: 实现浅复制需要使用Object类的MemberwiseClone方法用于创建一个浅表副本
深复制: 须实现 ICloneable接口中的Clone方法,且需要需要克隆的对象加上[Serializable]特性
class DrawBase:System.Object , ICloneable
{
public string name = "jmj";
public DrawBase()
{
}
public object Clone()
{
return this as object; //引用同一个对象
return this.MemberwiseClone(); //浅复制
return new DrawBase() as object;//深复制
}
}
class Program
{
static void Main(string[] args)
{
DrawBase rect = new DrawBase();
Console.WriteLine(rect.name);
DrawBase line = rect.Clone() as DrawBase;
line.name = "a9fs3";
Console.WriteLine(rect.name);
DrawBase ploy = line.Clone() as DrawBase;
ploy.name = "lj";
Console.WriteLine(rect.name);
Console.WriteLine(object.ReferenceEquals(line, ploy));
Console.ReadLine();
}
}
运行结果:
return this as object; //引用同一个对象
输出:jmj
a9fs3
lj
True
return this.MemberwiseClone(); //浅复制
return new DrawBase() as object;//深复制
输出均为: jmj
jmj
jmj
False
解释:
return this as object 方法总是引用同一个对象,因此相应的堆内存上的值会改变!
后两种方法都是对对象的复制,区别在于复制的类别不同:深复制会复制整个填充的对象,包括该对象中其他引用类型和值类型的值;而浅复制只复制了一个对象中所有引用,它没有值的复制,通过引用它们的其他对象的引用来共享它们。
--------------------------------------------------------------------------------------------------------
/// BaseObject类是一个用来继承的抽象类。
/// 每一个由此类继承而来的类将自动支持克隆方法。
/// 该类实现了Icloneable接口,并且每个从该对象继承而来的对象都将同样地
/// 支持Icloneable接口。
/// </summary>
public abstract class BaseObject : ICloneable
{
/// <summary>
/// 克隆对象,并返回一个已克隆对象的引用
/// </summary>
/// <returns> 引用新的克隆对象 </returns>
public object Clone()
{
// 首先我们建立指定类型的一个实例
object newObject = Activator.CreateInstance( this .GetType());
// 我们取得新的类型实例的字段数组。
FieldInfo[] fields = newObject.GetType().GetFields();
int i = 0 ;
foreach (FieldInfo fi in this .GetType().GetFields())
{
// 我们判断字段是否支持ICloneable接口。
Type ICloneType = fi.FieldType.
GetInterface( " ICloneable " , true );
if (ICloneType != null )
{
// 取得对象的Icloneable接口。
ICloneable IClone = (ICloneable)fi.GetValue( this );
// 我们使用克隆方法给字段设定新值。
fields[i].SetValue(newObject, IClone.Clone());
}
else
{
// 如果该字段部支持Icloneable接口,直接设置即可。
fields[i].SetValue(newObject, fi.GetValue( this ));
}
// 现在我们检查该对象是否支持IEnumerable接口,如果支持,
// 我们还需要枚举其所有项并检查他们是否支持IList 或 IDictionary 接口。
Type IEnumerableType = fi.FieldType.GetInterface( " IEnumerable " , true );
if (IEnumerableType != null )
{
// 取得该字段的IEnumerable接口
IEnumerable IEnum = (IEnumerable)fi.GetValue( this );
// 这个版本支持IList 或 IDictionary 接口来迭代集合。
Type IListType = fields[i].FieldType.GetInterface( " IList " , true );
Type IDicType = fields[i].FieldType.GetInterface( " IDictionary " , true );
int j = 0 ;
if (IListType != null )
{
// 取得IList接口。
IList list = (IList)fields[i].GetValue(newObject);
foreach ( object obj in IEnum)
{
// 查看当前项是否支持支持ICloneable 接口。
ICloneType = obj.GetType().
GetInterface( " ICloneable " , true );
if (ICloneType != null )
{
// 如果支持ICloneable 接口,
// 我们用它李设置列表中的对象的克隆
ICloneable clone = (ICloneable)obj;
list[j] = clone.Clone();
}
// 注意:如果列表中的项不支持ICloneable接口,那么
// 在克隆列表的项将与原列表对应项相同
// (只要该类型是引用类型)
j ++ ;
}
}
else if (IDicType != null )
{
// 取得IDictionary 接口
IDictionary dic = (IDictionary)fields[i].
GetValue(newObject);
j = 0 ;
foreach (DictionaryEntry de in IEnum)
{
// 查看当前项是否支持支持ICloneable 接口。
ICloneType = de.Value.GetType().
GetInterface( " ICloneable " , true );
if (ICloneType != null )
{
ICloneable clone = (ICloneable)de.Value;
dic[de.Key] = clone.Clone();
}
j ++ ;
}
}
}
i ++ ;
}
return newObject;
}
}
url:http://greatverve.cnblogs.com/archive/2011/07/15/csharp-clone.html