2013-03-09 51 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