2010-07-02 74 views

回答

52

請嘗試以下

if TypeOf x Is IFoo Then 
    ... 
+1

謝謝/ TypeOf關鍵字是我失蹤 – Tahbaza 2010-07-02 17:10:57

6

像這樣:

If TypeOf x Is IFoo Then 
1

直接翻譯是:

If TypeOf x Is IFoo Then 
    ... 
End If 

但(回答你的第二個問題),如果原始代碼是更好的寫法如下

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

然後,是的,

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

更好。

相關問題