2013-03-09 50 views
1

民間;如何在繼承的共享方法中獲取類的類型

代碼如下所示:

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

你可以用通用的基類獲得所需的行爲,我的VB有點生疏,所以你可能會發現流浪的零星或括號。這實際上是獲得對共享基類函數中的繼承類的類型引用的唯一方式。

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