.net - 在基础对象中构造派生类型的对象

标签 .net vb.net class

vb.net 中是否可以有一个方法来构造 bass 类中任何派生类的对象? 在此代码中,x.Clone 应返回一个 Bar 对象。 这是使用两种不同对象类型在两个类中重复代码的唯一方法。

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

最佳答案

答案具体取决于您在克隆方法中想要实现的目标。

如果您想创建当前类的新实例而不实际复制任何属性(听起来您可能有兴趣根据示例代码和描述进行操作),那么简单的解决方案是:

Public Class Foo
    Implements ICloneable

    Public Function Clone() As Object Implements System.ICloneable.Clone
        Return Activator.CreateInstance(Me.GetType)
    End Function
End Class

您可以通过在 Bar 中添加消息(或某种调试或输出)来测试是否调用此函数:

Public Class Bar
    Inherits Foo

    Public Sub New()
        MsgBox("Test")
    End Sub

End Class

如果您有Option Strict On(我强烈推荐),则 main 中的代码将是:

Sub Main()
    Dim x As New Bar
    Dim y As Bar = DirectCast(x.Clone, Bar)
End Sub

但是,如果您有兴趣从当前类复制成员值,则可以使用 MemberwiseClone :

Public Class Foo
    Implements ICloneable

    Public Function Clone() As Object Implements System.ICloneable.Clone
        Return Me.MemberwiseClone
    End Function
End Class

但是,这只会创建浅复制,将复制引用,并且不会调用 Bar 上的构造函数。当我们以这种方式使用 MemberwiseClone 时,我们总是添加一个可重写的方法,继承者可以使用该方法来执行克隆后清理:

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

最后,如果您不喜欢浅复制或处理复制的引用,您可以使用一种廉价但非常有效的技巧来执行深复制:使用 BinaryFormatter 进行序列化和反序列化:

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

这要求类可序列化,但这很容易做到。

关于.net - 在基础对象中构造派生类型的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14086684/

相关文章:

c# - 如何为 MVVM 样板代码扩展 Prism

vb.net - 如何在VB.NET中挖掘异常数据

javascript - 将键和原型(prototype)方法转换为驼峰式

c# - 让类返回一个字段

Python inst/klass 而不是 self ?

c# - System.Text.Json 不能用对象数组反序列化一个对象?

c# - 如何删除 XDocument 中非根节点的 xmlns 属性?

c# - 如何访问 C# 项目中 App_Data 文件夹中的嵌入式 xml?

c# - 消除 visual studio 解决方案文件夹中的临时文件

vb.net - 获取突出显示的文本