2013-03-09 2 views
1

Folks;상속 된 공유 메서드에서 클래스 형식을 얻는 방법

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 

OK :

코드처럼 보인다. 문제는 다른 클래스에서 상속 된 공유 함수 내에서 클래스 유형을 가져 오는 방법이 있습니까?

저는 XML serializer/desrializer를 상속 가능한 클래스로 구축하여 상속받은 클래스를 XML 파일로 다시 세리 할 수 ​​있으며 다시 사용할 수 있습니다. 이를 위해 각 클래스 유형에 대한 serializer/deserializer를 작성하는 대신 기능을 상속 받고 싶습니다.

그렇지만 공유 기능에서 나를 계승 한 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 

은 오히려 자신의 시리얼 구현 또는 XmlSerializer

을 통해보다 XmlSerialization을 처리하기 위해 오히려 이상한 솔루션처럼 보이지 않습니다
관련 문제