To check if object is an array and loop over it in C#
public object value { get; set; }
Recently stuck with to check object is array or not. if an object is an array then convert that array to comm separated string .
if (objArray != null)
{
string ElementValue = "";
var objArrayType = objArray.GetType();
if (objArrayType .IsArray)
{
foreach (object o in (Array)objArray)
{
ElementValue = ElementValue + "," + o;
}
ElementValue = ElementValue.Substring(1);
}
else
{
ElementValue = Convert.ToString(item.value);
}
var objArrayType = objArray.GetType();
if (objArrayType .IsArray)
{
foreach (object o in (Array)objArray)
{
ElementValue = ElementValue + "," + o;
}
ElementValue = ElementValue.Substring(1);
}
else
{
ElementValue = Convert.ToString(item.value);
}
}