I have the situation where I am given an object and need to:
- Determine if that object is a single object or a collection (Array, List, etc)
- If it is a collection, step though the list.
What I have so far. Testing for IEnumerable does not work. And the conversion to IEnumerable only works for non-primitive types.
static bool IsIEnum<T>(T x)
{
return null != typeof(T).GetInterface("IEnumerable`1");
}
static void print(object o)
{
Console.WriteLine(IsIEnum(o)); // Always returns false
var o2 = (IEnumerable<object>)o; // Exception on arrays of primitives
foreach(var i in o2) {
Console.WriteLine(i);
}
}
public void Test()
{
//int [] x = new int[]{1,2,3,4,5,6,7,8,9};
string [] x = new string[]{"Now", "is", "the", "time..."};
print(x);
}
Anyone know how to do this?