为实现这个目的,Visual Basic .NET在System.ICloneable里提供了一个标准的接口。.NET框架里的很多对象都能够识别并使用ICloneable接口,从而进行对对象的深度复制。
下面的代码是实现ICloneable接口的一个例子:
Public Class CopyMe
Implements ICloneable
Private strValue = "Yes"
Public Function Clone() As Object Implements _
System.ICloneable.Clone
Dim objCopy As CopyMe
objCopy = New CopyMe()
objCopy.Value = Me.Value
Return objCopy
End Function
Public Property Value() As String
Get
Return
strValue
End Get
Set(ByVal Value As String)
strValue
= Value
End Set
End Property
End Class
Clone这个方法为对象创建了一个新的实例,并将其属性的值按照当前对象的值来设置,再将其返回。