I have a dynamic object that sometimes is an object and sometimes is an object[].
How can I check if the dynamic object is an array?
Use Type.IsArray:
From MSDN:
int [] array = {1,2,3,4};
Type t = array.GetType();
// t.IsArray == true
Console.WriteLine("The type is {0}. Is this type an array? {1}", t, t.IsArray);
To complement Rango's original response, a more generic way to determine is to use the type's IsSerializable property. Because if the object is a List or any other collection, the IsArray returns false.
int [] array = {1,2,3,4};
Type t1 = array.GetType();
// t1.IsArray == true
List<int> list = new List();
list.AddRange(array);
Type t2 = list.GetType();
//t2.IsArray = false;
//t2.IsSerializable = true;
foreach(var i in list) {
// do stuff
}
"some text".GetType().IsSerializable is also true but is not an array or list