13

C#の質問の精神で..

VB.NET でクラス タイプを比較する同等のステートメントは何ですか?

4

3 に答える 3

19

のようなものをお探しTypeOfですか? これは int/etc ではなく、参照型でのみ機能します。

If TypeOf "value" Is String Then
     Console.WriteLine("'tis a string, m'lord!")

それとも、変数の 2 つの異なるインスタンスを比較しますか? ref 型でも機能します。

Dim one As Object = "not an object"
Dim two As Object = "also not an object, exactly"
Dim three as Object = 3D

If one.GetType.Equals( two.GetType ) Then WL("They are the same, man")
If one.GetType Is two.GetType then WL("Also the same")
If one.GetType IsNot three.GetType Then WL("but these aren't")

gettype()2 つのオブジェクトを使用していない場合は、次のように使用することもできます。

If three.GetType Is gettype(integer) then WL("is int")

何かが別のタイプのサブクラスであるかどうかを確認したい場合 (および .net 3.5 にある場合):

If three.GetType.IsSubclassOf(gettype(Object)) then WL("it is")

しかし、以前のバージョンでそれを行いたい場合は、それを反転して (見た目が奇妙です)、次を使用する必要があります。

If gettype(Object).IsAssignableFrom(three.GetType) Then WL("it is")

これらはすべてSnippetCompilerでコンパイルされるため、お持ちでない場合は DL してください。

于 2009-06-17T00:01:51.607 に答える
4
TypeOf obj Is MyClass
于 2009-06-16T23:58:57.697 に答える
0

リンクされた質問に相当する VB はほぼ同じです。

Dim result As Boolean = obj.GetType().IsAssignableFrom(otherObj.GetType())
于 2009-06-16T23:59:04.233 に答える