How to determine if “object is IEnumerable” (C# syntax) in VB.NET?

I've seen a lot of posts for how to do this in C#, but I'm working in VB and they don't work and I can't seem to get them to convert. Anyone know this? C# is something like:

if (x is IEnumerable)
{

}

I'm looking to see if an object passed to my function implements IEnumerable so I can treat it like a collection.

I've already tried "x Is IEnumerable" and VB won't allow it anywhere... it expects a type of IEnumerable, such as IEnumerable(Of String).

Answers


Even better, use TryCast. TryCast Operator

Dim collection As IEnumerable = TryCast(x, IEnumerable)
If collection IsNot Nothing Then
   ' do something
End If

See the TypeOf ... Is ... operator:

If TypeOf obj Is IEnumerable Then

The VB equivalent would be:

If TypeOf x Is IEnumerable Then

Need Your Help

More terminals on cygwin

cygwin

I want to have one more terminal on cygwin.

Trouble converting a console application to a WPF application in VS 2010

c# .net wpf console port

I created a console application that I later decided would function better as a WPF application. I changed the output type to Windows Application and added what I believe are the necessary referenc...