パラメータの1つがインターフェイスである場合、Visual Studioは型チェック関数のパラメータを停止しているようです。VB.Netの関数インターフェイスパラメータを使った型チェックfun
は、次のことを考えてみましょう:
' An interface and the class that implements it:
Public Interface IA
End Interface
Public Class A
Implements IA
End Class
' Another reference type for the demonstration:
Public Class MyReferenceType
End Class
' A function that uses IA as the type of one of its parameters:
Private Function SomeFunc(ByVal a As IA, ByVal r As MyReferenceType)
Return Nothing
End Sub
そしてここでは、型チェックの問題の一例です:
Private Sub Example()
Dim a As IA = New A
Dim r As New MyReferenceType
' Some other random reference type, choose any
' other reference type you like
Dim list As New List(Of String)
' Each of these calls to SomeFunc compile without errors.
SomeFunc(r, r)
SomeFunc(r, a)
SomeFunc(list, r)
SomeFunc(list, a)
' Does not compile due to type mismatch
'SomeFunc(list, list)
End Sub
私のコメントが示唆するように、このコードは、いずれかのエディタでエラーなしで、細かいコンパイル。プログラムを実行すると、私はSystem.InvalidCastException
になりますが、これはまったく驚くことではありません。私はこれがコンパイラの型チェックのバグだと思いますか?私はVisual Studio 2005を使用しているので、これはVSの後のバージョンで修正されていますか?
あなたは正しいです!私は何とかOption Strictをオフにしたに違いありません。MSDNはデフォルト状態がオンであると言います。 – AuGambit