民间;
代码如下:
Public Class MasterA
Inherits Underling
End Class
Public Class MasterB
Inherits Underling
End Class
Public Mustinherit Class Underling
Sub DoSomething()
Me.GetType 'Using the instance, I can get the class.
end sub
Shared function() as ???? 'How can I define the return type based on the class that inherited me?
'Me.GetType 'Won't work as this is a shared function with no instance 'Me'
End Function
End class
行。问题是:有没有办法从另一个类继承的共享函数中获取类类型?
我正在构建的是一个XML序列化程序/解析器作为可继承的类,以便继承它的类可以被提升为XML文件,然后再返回。我不想为每个类型的类编写一个序列化器/反序列化器,而是希望继承该功能。
要做到这一点,要求我能够确定在共享功能中继承我的clas。
答案 0 :(得分:0)
你可以使用通用基类获得所需的行为,我的VB有点生疏,所以你可能会发现流浪的parens或括号。这实际上是获取共享基类函数中继承类的类型引用的唯一方法。
Public Mustinherit Class Underling(Of T)
Sub DoSomething()
Me.GetType 'Using the instance, I can get the class.
end sub
Shared function() As T
' GetType(T) should get the type at this point
End Function
End class
Public Class MasterA
Inherits Underling(Of MasterA)
End Class
Public Class MasterB
Inherits Underling(Of MasterB)
End Class
作为旁注,处理XmlSerialization
而不是通过您自己的序列化程序实现或XmlSerializer