If you're using a recent version of the C# compiler, you can use a pattern-matching
switch
to make sure the property value is of the correct type:
switch (results)
{
case null:
case IList _:
case IListSource _:
{
dataGridView1.DataSource = results;
break;
}
case ICollection collection:
{
dataGridView1.DataSource = new ArrayList(collection);
break;
}
case IEnumerable sequence:
{
dataGridView1.DataSource = sequence.Cast<object>().ToList();
break;
}
default:
{
dataGridView1.DataSource = null;
break;
}
}
It would be simple enough to convert this to work in an older compiler, but it wouldn't be as clean. :)