2009-06-16 8 views

Respuesta

14

¿Usted está buscando algo así como TypeOf? Esto solo funciona con tipos de referencia, no int/etc.

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

¿O desea comparar dos instancias de variables diferentes? También funciona para este tipo de 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") 

Usted podría también utilizar gettype() como así, si usted no está utilizando dos objetos:

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

Si usted quiere ver si algo es una subclase de otro tipo (y están en .net 3.5):

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

Pero si usted quiere hacer que en las versiones anteriores, hay que darle la vuelta (raro mirar) y uso:

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

Todos estos compilar en SnippetCompiler, por lo tanto, vaya DL si no lo tiene.

3
TypeOf obj Is MyClass 
0

La VB equivalente a su pregunta vinculada es casi idéntica:

Dim result As Boolean = obj.GetType().IsAssignableFrom(otherObj.GetType()) 
Cuestiones relacionadas