我试图查看datagrid.datasource是否属于特定类型,然后采取不同的操作。
if grid.datasource is CollectionBase then
' do sone thing
else if grid.datasource is IEnumerable then
' do other thing
end if
第一次检查给我CollectionBase是一个类型,不能用于表达式。这是什么意思?
更新1:
我检查过,似乎我正在向网格发送一组对象。客户[]之类的东西。我怎样才能使它成为通用的所以我可以获得数组并以某种方式得到计数。
答案 0 :(得分:1)
试试这个
if grid.datasource.GetType() is GetType(CollectionBase) then
Dim myCollection as CollectionBase = TryCast(grid.DataSource, CollectionBase)
If (myCollection IsNot Nothing) Then
myCollection.Count
End If
else if grid.datasource.GetType() is GetType(IEnumerable) then
Dim myCollection as IEnumerable= TryCast(grid.DataSource, IEnumerable)
If (myCollection IsNot Nothing) Then
myCollection.Count()
End If
end if
答案 1 :(得分:1)
您需要使用TypeOf … Is …
:
If TypeOf grid.datasource Is CollectionBase Then
' do sone thing
Else If TypeOf grid.datasource Is IEnumerable Then
' do other thing
End If
仅使用Is
检查两个对象的身份。但是,代码中的第二个操作数不是对象,它是类型名称。