Visual Basic equivalente a la comprobación de tipo C#


¿Cuál es el equivalente Visual Basic de la siguiente expresión booleana de C#?

data.GetType() == typeof(System.Data.DataView)

Nota: La variable data se declara como IEnumerable.

 43
Author: Peter Mortensen, 2010-04-13

4 answers

Según recuerdo

TypeOf data Is System.Data.DataView

Editar:
Como James Curran señaló, esto funciona si los datos son un subtipo de Sistema.Datos.DataView también.

Si desea restringir eso al Sistema.Datos.DataView solamente, esto debería funcionar:

data.GetType() Is GetType(System.Data.DataView)
 70
Author: Powerlord,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-04-12 20:28:35

Solo pensé en publicar un resumen para el beneficio de los programadores de C#:

C # val is SomeType

En VB.NET: TypeOf val Is SomeType

A diferencia de Is, esto solo puede ser negado como Not TypeOf val Is SomeType

C # typeof(SomeType)

En VB.NET: GetType(SomeType)

C # val.GetType() == typeof(SomeType)

En VB.NET: val.GetType() = GetType(SomeType)

(aunque Is también funciona, ver a continuación)

C # val.ReferenceEquals(something)

En VB.NET: val Is something

Se puede negar muy bien: val IsNot something


C # val as SomeType

En VB.NET: TryCast(val, SomeType)

C # (SomeType) val

En VB.NET: DirectCast(val, SomeType)

(excepto cuando los tipos involucrados implementan un operador de fundición)

 33
Author: Roman Starkov,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-12-14 13:41:24

También puedes usar TryCast y luego comprobar si no hay nada, de esta manera puedes usar el tipo fundido más adelante. Si no necesitas hacer eso, no lo hagas de esta manera, porque otros son más eficientes.

Vea este ejemplo:

VB:

    Dim pnl As Panel = TryCast(c, Panel)
    If (pnl IsNot Nothing) Then
        pnl.Visible = False
    End If

C#

Panel pnl = c as Panel;
if (pnl != null) {
    pnl.Visible = false;
}
 2
Author: Nick N.,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-07-02 09:55:18

Prueba esto.

GetType(Foo)
 1
Author: SubZero,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-04-12 20:19:28