While it's theoretically possible to use reflection to do this; since your explicit goal is just to handle rows or columns the easiest option is to just create two overloads for the function:
private void SelectNew(DataGridViewColumnCollection collection, int index)
{
ClearSelection();
collection[index].Selected = true;
}
private void SelectNew(DataGridViewRowCollection collection, int index)
{
ClearSelection();
collection[index].Selected = true;
}
If you tried to use reflection to do this it would work, but it would be slower, less readable, and has the danger of no compile time protection; people would be able to pass in other kinds of lists that didn't have a Selected
property and it would compile and just fail at runtime.