难道possable在vb.net中有这种构造的任何派生类对象中的低音类中的方法? 在这种code x.Clone应返回Bar对象。 是dulpicate的code在这两个类的与两个不同的对象类型的唯一方法。
模块模块1
副主()
昏暗x As中新的吧
昏暗ÿ,酒吧= x.Clone
结束小组
前端模块
公共类Foo
实现ICloneable
公共功能的clone()为对象实现System.ICloneable.Clone
克隆= Me.new()编译失败不会在构造函数
克隆=新富'建在派生类的新富
端功能
末级
公共类酒吧
继承富
额外的实施
但没有加入域
末级
解决方案
答案取决于你所要完成的克隆方法具体是什么。
如果你想创建当前类的新实例,而无需实际复制任何属性(这是听起来像你可能有兴趣根据样本code和描述那样),那么简单的解决方案是
公共类Foo
实现ICloneable
公共功能的clone()为对象实现System.ICloneable.Clone
返回Activator.CreateInstance(Me.GetType)
端功能
末级
您可以测试这种被称为在酒吧添加消息(或某种调试或输出):
公共类酒吧
继承富
公共子新()
MSGBOX(测试)
结束小组
末级
如果你有选项严
,我强烈建议,在code的主要是:
子的Main()
昏暗x As中新的吧
昏暗ÿ,酒吧= DirectCast(x.Clone,酒吧)
结束小组
不过,如果您有兴趣从当前类复制成员的值,可以使用的 MemberwiseClone :
公共类Foo
实现ICloneable
公共功能的clone()为对象实现System.ICloneable.Clone
返回Me.MemberwiseClone
端功能
末级
然而,这只会造成一个浅拷贝,将复制引用,并不会调用上栏的构造。当我们使用MemberwiseClone以这种方式,我们总是加可重写方法,可以通过继承用于执行克隆后清理:
公共类Foo
实现ICloneable
公共功能的clone()为对象实现System.ICloneable.Clone
昏暗oObject作为富
oObject = DirectCast(Me.MemberwiseClone,富)
oObject.PostCloneCleanup()
返回oObject
端功能
受保护的可重写小组PostCloneCleanup()
结束小组
末级
公共类酒吧
继承富
公共子新()
MSGBOX(测试)
结束小组
受保护的覆盖子PostCloneCleanup()
MSGBOX(PostCloneCleanup)
结束小组
末级
序列化和反序列化使用BinaryFormmater:
最后,如果浅复制或处理复制引用是不是你喜欢,你可以使用一个便宜,但非常有效的一招进行深拷贝
公共功能CreateDeepCopy(Of T)中(BYVAL oRecord为t)为t
如果oRecord是没有那么
返回任何结果
结束如果
如果不oRecord.GetType.IsSerializable然后
抛出新的ArgumentException(oRecord.GetType.ToString&安培;是不是可序列化)
结束如果
昏暗oFormatter作为新System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
使用ostream的作为IO.MemoryStream =新IO.MemoryStream
oFormatter.Serialize(ostream的,oRecord)
oStream.Position = 0
返回DirectCast(oFormatter.Deserialize(ostream的),T)
结束使用
端功能
这要求类可序列化,但是这是很容易做到的。
Is it possable in vb.net to have a method that constructs an object of any derived class in the bass class? In this code x.Clone should return a Bar object. Is the only way to dulpicate the code in both class's with both different object types.
Module Module1
Sub Main()
Dim x As New Bar
Dim y As Bar = x.Clone
End Sub
End Module
Public Class Foo
Implements ICloneable
Public Function Clone() As Object Implements System.ICloneable.Clone
Clone = Me.new() 'fails to compile as not in a constructor
Clone = new Foo 'constructs a new foo in the derived class
End Function
End Class
Public Class Bar
Inherits Foo
'additional implementation
'but no addition fields
End Class
解决方案
The answer depends on specifically what you are trying to accomplish in the cloning method.
If you want to create a new instance of the current class without actually copying any of the properties (which is sounds like you may be interested in doing based on the sample code and description), then the simple solution is:
Public Class Foo
Implements ICloneable
Public Function Clone() As Object Implements System.ICloneable.Clone
Return Activator.CreateInstance(Me.GetType)
End Function
End Class
You can test that this is called by adding a message (or some sort of debug or output) in Bar:
Public Class Bar
Inherits Foo
Public Sub New()
MsgBox("Test")
End Sub
End Class
If you have Option Strict On
, which I strongly recommend, the code in main would be:
Sub Main()
Dim x As New Bar
Dim y As Bar = DirectCast(x.Clone, Bar)
End Sub
However, if you are interested in copying the member values from the current class, you can use MemberwiseClone:
Public Class Foo
Implements ICloneable
Public Function Clone() As Object Implements System.ICloneable.Clone
Return Me.MemberwiseClone
End Function
End Class
However, this will only create a shallow copy, will copy references, and will not invoke the constructor on Bar. When we use MemberwiseClone in this manner, we always add an overridable method that can be used by inheritors to perform post-clone cleanup:
Public Class Foo
Implements ICloneable
Public Function Clone() As Object Implements System.ICloneable.Clone
Dim oObject As Foo
oObject = DirectCast(Me.MemberwiseClone, Foo)
oObject.PostCloneCleanup()
Return oObject
End Function
Protected Overridable Sub PostCloneCleanup()
End Sub
End Class
Public Class Bar
Inherits Foo
Public Sub New()
MsgBox("Test")
End Sub
Protected Overrides Sub PostCloneCleanup()
MsgBox("PostCloneCleanup")
End Sub
End Class
Finally, if shallow copying or dealing with copied references is not to you liking, you can perform a deep copy using a cheap, but very effective trick: serialization and deserialization using the BinaryFormmater:
Public Function CreateDeepCopy(Of T)(ByVal oRecord As T) As T
If oRecord Is Nothing Then
Return Nothing
End If
If Not oRecord.GetType.IsSerializable Then
Throw New ArgumentException(oRecord.GetType.ToString & " is not serializable")
End If
Dim oFormatter As New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
Using oStream As IO.MemoryStream = New IO.MemoryStream
oFormatter.Serialize(oStream, oRecord)
oStream.Position = 0
Return DirectCast(oFormatter.Deserialize(oStream), T)
End Using
End Function
This requires that the classes be serializable, but that is easy to do.