我有一个对象参数,需要检查该对象是否实现了指定接口,在 vb.net 中如何进行测试?
谢谢。
我有一个对象参数,需要检查该对象是否实现了指定接口,在 vb.net 中如何进行测试?
谢谢。
使用 TypeOf...Is:
If TypeOf objectParameter Is ISpecifiedInterface Then
'do stuff
End If
if (typeof(IWhateverable).IsAssignableFrom(myType)) { ... }
VB.Net
Dim _interfaceList As List(Of Type) = myInstance.GetType().GetInterfaces().ToList()
If _interfaceList.Contains(GetType(IMyInterface)) Then
'Do the stuff
End If
以下是一种简单的方法,用于确定给定的对象变量“o”是否实现了特定接口“ISomething”:
If o.GetType().GetInterfaces().Contains(GetType(ISomething)) Then
' The interface is implemented
End If
requiredInterface.IsAssignableFrom(representedType)
其中requiredInterface和representedType都是类型(Types)
我有一个 List(Of String)
,而 TypeOf tmp Is IList
返回 False
。List(Of T) 实现了多个接口(IEnumerable、IList 等),仅检查其中一个需要在 VB 中使用以下代码片段:
If tmp.GetInterfaces().Contains(GetType(IEnumerable)) Then
// do stuff...
End If