直接赋值
将a的引用复制给b,实际操作的是同一个对象,当修改zhangsan的属性时,李四的属性也随之改变,同样lisi的属性修改后,zhangsan的属性也会修改。
案例
Person zhangsan = new Person()
{
Name = "张三",
Age = 28
};
Person lisi = new Person()
{
Name = "李四",
Age = 29
};
lisi = zhangsan;
Console.WriteLine("zhangsan==Name:{0},Age:{1}", zhangsan.Name,zhangsan.Age);
Console.WriteLine("lisi==Name:{0},Age:{1}", lisi.Name,lisi.Age);
Console.WriteLine("-----------------------修改lisi的属性值-----------------------");
lisi.Name = "李四";
lisi.Age = 30;
Console.WriteLine("zhangsan==Name:{0},Age:{1}", zhangsan.Name, zhangsan.Age);
Console.WriteLine("lisi==Name:{0},Age:{1}", lisi.Name, lisi.Age);
利用反射拷贝
- 深拷贝
拷贝一个对象时,不仅仅把对象的引用进行复制,还把该对象引用的值也一起拷贝。深拷贝的拷贝对象就和源对象互相独立,其中任何一个对象的改动都不会对另外一个对象造成影响。
深拷贝代码
public static T DeepCopyByReflect<T>(T obj)
{
//如果是字符串或值类型则直接返回
if (obj == null || (obj is string) || (obj.GetType().IsValueType)) return obj;
object retval = Activator.CreateInstance(obj.GetType());
FieldInfo[] fields = obj.GetType().GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static);
foreach (FieldInfo field in fields)
{
try { field.SetValue(retval, DeepCopyByReflect(field.GetValue(obj))); }
catch { }
}
return (T)retval;
}
案例
将张三的对象复制给李四后,修改李四的属性值时,张三的属性值并未改变。
Person zhangsan = new Person()
{
Name = "张三",
Age = 28
};
Person lisi = new Person()
{
Name = "李四",
Age = 29
};
lisi=DeepCopyByReflect<Person>(zhangsan);
Console.WriteLine("zhangsan==Name:{0},Age:{1}", zhangsan.Name, zhangsan.Age);
Console.WriteLine("lisi==Name:{0},Age:{1}", lisi.Name, lisi.Age);
Console.WriteLine("-----------------------修改lisi的属性值-----------------------");
lisi.Name = "李四";
lisi.Age = 30;
Console.WriteLine("zhangsan==Name:{0},Age:{1}", zhangsan.Name, zhangsan.Age);
Console.WriteLine("lisi==Name:{0},Age:{1}", lisi.Name, lisi.Age);
- 浅拷贝
和直接赋值一样,仅仅拷贝对象的引用进行拷贝,实际操作的是同一个对象,当修改zhangsan的属性时,李四的属性也随之改变,同样lisi的属性修改后,zhangsan的属性也会修改。
浅拷贝代码
FieldInfo[] fields = typeof(TSource).GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
for (int i = 0; i < fields.Length; i++)
{
FieldInfo field = fields[i];
field.SetValue(targetVal, field.GetValue(sourceVal));
}
PropertyInfo[] properties = typeof(TSource).GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
for (int i = 0; i < properties.Length; i++)
{
PropertyInfo property = properties[i];
property.SetValue(targetVal, property.GetValue(sourceVal, null), null);
}
案例
Person zhangsan = new Person()
{
Name = "张三",
Age = 28
};
Person lisi = new Person()
{
Name = "李四",
Age = 29
};
lisi = zhangsan;
Console.WriteLine("zhangsan==Name:{0},Age:{1}", zhangsan.Name,zhangsan.Age);
Console.WriteLine("lisi==Name:{0},Age:{1}", lisi.Name,lisi.Age);
Console.WriteLine("-----------------------修改lisi的属性值-----------------------");
lisi.Name = "李四";
lisi.Age = 30;
ShallowCopy<Person, Person>(lisi,zhangsan);
Console.WriteLine("zhangsan==Name:{0},Age:{1}", zhangsan.Name, zhangsan.Age);
Console.WriteLine("lisi==Name:{0},Age:{1}", lisi.Name, lisi.Age);
版权声明:本文为jh_negit原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。