2010-07-02 15 views

Odpowiedz

52

Wypróbuj następujące

if TypeOf x Is IFoo Then 
    ... 
+1

dzięki/słowo kluczowe TypeOf jest tym, czego mi brakowało – Tahbaza

1

Tłumaczenie bezpośredni jest:

If TypeOf x Is IFoo Then 
    ... 
End If 

Ale (aby odpowiedzieć na pytanie drugie), jeśli oryginalny kod został lepiej napisany jako

var y = x as IFoo; 
if (y != null) 
{ 
    ... something referencing y rather than (IFoo)x ... 
} 

Następnie, tak,

Dim y = TryCast(x, IFoo) 
If y IsNot Nothing Then 
    ... something referencing y rather than CType or DirectCast (x, IFoo) 
End If 

jest lepiej.

Powiązane problemy