Je dois vérifier si un object donné implémente une interface. En C # je dirais simplement:
if (x is IFoo) { }
Est-ce que utiliser un TryCast()
et ensuite vérifier la meilleure solution?
Essayez ce qui suit
if TypeOf x Is IFoo Then ...
Comme ça:
If TypeOf x Is IFoo Then
Utilisez cette solution Web en ligne pour convertir C # en VB.NET, ainsi que plusieurs autres conversions de code.
La traduction directe est:
If TypeOf x Is IFoo Then ... End If
Mais (pour répondre à votre deuxième question) si le code original était mieux écrit
var y = x as IFoo; if (y != null) { ... something referencing y rather than (IFoo)x ... }
Alors oui,
Dim y = TryCast(x, IFoo) If y IsNot Nothing Then ... something referencing y rather than CType or DirectCast (x, IFoo) End If
est mieux.
http://www.developerfusion.com/tools/convert/csharp-to-vb/ est également un excellent outil de conversion.