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 ファイルにシリアライズして、再び戻すことができます。これを行いたいクラスのタイプごとにシリアライザー/デシリアライザーを作成するのではなく、機能を継承したいと思います。

ただし、そのためには、共有関数で私を継承したクラスを確認できる必要があります。

4

1 に答える 1

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を使用するのではなく、処理するのはかなり奇妙なソリューションのように思えます

于 2013-03-09T04:00:25.887 に答える