对象复制(所谓的克隆-Clone)

对象复制,最generic的方法,估计是继承ICloneable,然后写Clone函数。

但当该函数不能修改,如是第三方组件中的类,或者因为其它原因,我们就被迫采取别的方法了。

如果碰巧对象实现了ISerializable,那么我们可以:

None.gif
None.gif        
private   static   void  TestClone()
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            Person p1 
= new Person();
InBlock.gif            p1.Age 
= 26;
InBlock.gif            p1.UserName 
= "灵感之源";
InBlock.gif
InBlock.gif            Person p2 
= (Person)CloneObjectEx(p1);
InBlock.gif            p2.UserName 
= "unruledboy";
InBlock.gif
InBlock.gif            Console.WriteLine(p1.UserName);
InBlock.gif            Console.WriteLine(p2.UserName);
ExpandedBlockEnd.gif        }

None.gif
None.gif        
public   static  Person CloneObject(Person ObjectInstance)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            BinaryFormatter bFormatter 
= new BinaryFormatter();
InBlock.gif            MemoryStream stream 
= new MemoryStream();
InBlock.gif            bFormatter.Serialize(stream, ObjectInstance);
InBlock.gif            stream.Seek(
0, SeekOrigin.Begin);
InBlock.gif            Person newObject 
= (Person)bFormatter.Deserialize(stream);
InBlock.gif            
return newObject;
ExpandedBlockEnd.gif        }

None.gif
None.gif        
public   static   object  CloneObjectEx( object  ObjectInstance)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            BinaryFormatter bFormatter 
= new BinaryFormatter();
InBlock.gif            MemoryStream stream 
= new MemoryStream();
InBlock.gif            bFormatter.Serialize(stream, ObjectInstance);
InBlock.gif            stream.Seek(
0, SeekOrigin.Begin);
InBlock.gif            
return bFormatter.Deserialize(stream);
ExpandedBlockEnd.gif        }

None.gif     [Serializable]
None.gif    
public   class  Person
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        
private int age;
InBlock.gif        
private string userName;
InBlock.gif
InBlock.gif        
public Person()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public int Age
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
getdot.gif{return age;}
ExpandedSubBlockStart.gifContractedSubBlock.gif            
setdot.gif{age = value;}
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public string UserName
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
getdot.gif{return userName;}
ExpandedSubBlockStart.gifContractedSubBlock.gif            
setdot.gif{userName = value;}
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }

CloneObject和CloneObjectEx的区别在于:CloneObject返回的是强类型,但限制为指定的类型,不通用;后者通用,但性能要更加低。


或者显式采用反射来逐个获取对象的每个属性:
Base class for cloning an object in C# 


如果对象连ISerializable都没有实现,那么我们只能:
How to serialize an object which is NOT marked as 'Serializable' using a surrogate


如果上述的方法都不满意,请你研究得出结果之后告诉我;)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值