読者です 読者をやめる 読者になる 読者になる

PHPC#VB.NETでtypeof演算子の意味が全然違っててめがっさ混乱するのでメモ。

・クラスの定義

Private Class MemberBase
    Public Sub doAfterSchoolActivity()
End Class

Private Class GorakubuMember
    Inherits MemberBase
    
    Public Sub doTeaBreak()
End Class

Private Class SeitokaiMember
    Inherits MemberBase

    Public Sub doIchaicha(ByVal aTarget As MemberBase)
End Class

Private Class Shujinko
    Public Sub doOpeningCall()
End Class

・本体

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    checkType(GetType(MemberBase), GetType(GorakubuMember), GetType(SeitokaiMember), New GorakubuMember, New SeitokaiMember)
End Sub


Private Sub checkType(ByVal aMemberBase As Type, _
                      ByVal aGorakubuMember As Type, _
                      ByVal aSeitokaiMember As Type, _
                      ByVal aKyouko As MemberBase, _
                      ByVal aAyano As MemberBase)
    Dim strList As New StringList

    Dim Yui As New GorakubuMember
    Dim Chitose As New SeitokaiMember
    Dim Akari As New Shujinko

    strList.Add("Yui.GetType Is Chitose.GetType")
    If Yui.GetType Is Chitose.GetType Then
        strList.Add("true")
    Else
        strList.Add("false")
    End If
    strList.Add("")

    strList.Add("Yui.GetType Is aKyouko.GetType")
    If Yui.GetType Is aKyouko.GetType Then
        strList.Add("true")
    Else
        strList.Add("false")
    End If
    strList.Add("")

    strList.Add("Yui.GetType Is aMemberBase")
    If Yui.GetType Is aMemberBase Then
        strList.Add("true")
    Else
        strList.Add("false")
    End If
    strList.Add("")

    strList.Add("Yui.GetType Is aGorakubuMember")
    If Yui.GetType Is aGorakubuMember Then
        strList.Add("true")
    Else
        strList.Add("false")
    End If
    strList.Add("")

    strList.Add("TypeOf (Yui) Is MemberBase")
    If TypeOf (Yui) Is MemberBase Then
        strList.Add("true")
    Else
        strList.Add("false")
    End If
    strList.Add("")

    strList.Add("TypeOf (Yui) Is GorakubuMember")
    If TypeOf (Yui) Is GorakubuMember Then
        strList.Add("true")
    Else
        strList.Add("false")
    End If
    strList.Add("")

    strList.Add("TypeOf (aKyouko) Is MemberBase")
    If TypeOf (aKyouko) Is MemberBase Then
        strList.Add("true")
    Else
        strList.Add("false")
    End If
    strList.Add("")

    strList.Add("TypeOf (aKyouko) Is GorakubuMember")
    If TypeOf (aKyouko) Is GorakubuMember Then
        strList.Add("true")
    Else
        strList.Add("false")
    End If
    strList.Add("")

    strList.Add("TypeOf (aKyouko) Is SeitokaiMember")
    If TypeOf (aKyouko) Is SeitokaiMember Then
        strList.Add("true")
    Else
        strList.Add("false")
    End If
    strList.Add("")

    strList.Add("aMemberBase Is Yui.GetType")
    If aMemberBase Is Yui.GetType Then
        strList.Add("true")
    Else
        strList.Add("false")
    End If
    strList.Add("")

    strList.Add("aGorakubuMember Is Yui.GetType")
    If aGorakubuMember Is Yui.GetType Then
        strList.Add("true")
    Else
        strList.Add("false")
    End If
    strList.Add("")

    strList.Add("aAyano.GetType Is aMemberBase")
    If aAyano.GetType Is aMemberBase Then
        strList.Add("true")
    Else
        strList.Add("false")
    End If
    strList.Add("")

    strList.Add("aAyano.GetType Is aSeitokaiMember")
    If aAyano.GetType Is aSeitokaiMember Then
        strList.Add("true")
    Else
        strList.Add("false")
    End If
    strList.Add("")

    strList.Add("TypeOf (aAyano) Is MemberBase")
    If TypeOf (aAyano) Is MemberBase Then
        strList.Add("true")
    Else
        strList.Add("false")
    End If
    strList.Add("")

    strList.Add("TypeOf (aAyano) Is GorakubuMember")
    If TypeOf (aAyano) Is GorakubuMember Then
        strList.Add("true")
    Else
        strList.Add("false")
    End If
    strList.Add("")

    strList.Add("TypeOf (aAyano) Is SeitokaiMember")
    If TypeOf (aAyano) Is SeitokaiMember Then
        strList.Add("true")
    Else
        strList.Add("false")
    End If
    strList.Add("")

    TextBox1.Text = strList.Text

End Sub

・実行結果

Yui.GetType Is Chitose.GetType
false

Yui.GetType Is aKyouko.GetType
true

Yui.GetType Is aMemberBase
false

Yui.GetType Is aGorakubuMember
true

TypeOf (Yui) Is MemberBase
true

TypeOf (Yui) Is GorakubuMember
true

TypeOf (aKyouko) Is MemberBase
true

TypeOf (aKyouko) Is GorakubuMember
true

TypeOf (aKyouko) Is SeitokaiMember
false

aMemberBase Is Yui.GetType
false

aGorakubuMember Is Yui.GetType
true

aAyano.GetType Is aMemberBase
false

aAyano.GetType Is aSeitokaiMember
true

TypeOf (aAyano) Is MemberBase
true

TypeOf (aAyano) Is GorakubuMember
false

TypeOf (aAyano) Is SeitokaiMember
true

え〜と、TypeOf () Is 文は継承関係を追ってくれるけど、 **.GetType Is **.GetType は継承関係を追わずに完全に同じクラスのインスタンスかどうかを比較する、ってことか。

あ、後、

    strList.Add("TypeOf (aAyano) Is GorakubuMember")
    If TypeOf (aAyano) Is GorakubuMember Then
        strList.Add("true")
    Else
        strList.Add("false")
    End If
    strList.Add("")

コンパイラを通ってfalseになるけど、

    strList.Add("TypeOf (Chitose) Is GorakubuMember")
    If TypeOf (Chitose) Is GorakubuMember Then
        strList.Add("true")
    Else
        strList.Add("false")
    End If
    strList.Add("")

コンパイラの時点ではねられます。余計なことを、イヤイヤ‥‥