문제

의 정신으로 씨# 의문..

vb.net의 클래스 유형을 비교하는 동등한 진술은 무엇입니까?

도움이 되었습니까?

해결책

당신은 같은 것을 찾고 있습니까? TypeOf? 이것은 int/등이 아닌 참조 유형에서만 작동합니다.

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

아니면 두 가지 다른 변수 인스턴스를 비교하고 싶습니까? 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() 따라서 두 객체를 사용하지 않는 경우 :

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로 가십시오.

다른 팁

TypeOf obj Is MyClass

링크 된 질문에 해당하는 VB는 거의 동일합니다.

Dim result As Boolean = obj.GetType().IsAssignableFrom(otherObj.GetType())
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top