2011-06-23 9 views
0

datagrid.datasourceが特定の型であるかどうかを確認しようとしています。VB.NETでのデータ型チェックの質問

if grid.datasource is CollectionBase then 
' do sone thing 
else if grid.datasource is IEnumerable then 
' do other thing 
end if 

最初のチェックで私はCollectionBaseが型であり、式として使用できません。どういう意味ですか?

UPDATE 1:

私がチェックし、私がグリッドにオブジェクトの配列を送信していそうです。顧客のようなもの[]どのようにしてそれを一般的なものにして、配列を取得して何らかの形でカウントを得ることができます。単に二つのオブジェクトのアイデンティティのためのIsチェックを使用して

If TypeOf grid.datasource Is CollectionBase Then 
' do sone thing 
Else If TypeOf grid.datasource Is IEnumerable Then 
' do other thing 
End If 

+0

私のアップデートはあなたのアップデートの質問に答えるはずです。 'TryCast'を使用すると、キャストが失敗した場合にnullを返すので、タイプアップの前にチェックする必要はありません。 – Jay

答えて

1

はあなたがTypeOf … Is …を使用する必要があり、この

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

を試してみてください。しかし、コードの第2オペランドはオブジェクトではなく、です。これは型名です。

+0

はもう1つの詳細で質問を更新しました –