I have a method that accepts a basic generic variable in the method signature:
public void Foo<T>(T val) {
if (val is IEnumerable) {
Bar(val)
}
}
...Logic if not enumerable
The signature for Bar looks like this:
private void Bar<T>(IEnumerable<T> vals) {
...Logic if enumerable
}
The logic is very lengthy and different based on whether the item passed in is enumerable or not. I can't get Bar(val)
to work. I tried the following, let me know what I'm missing:
Bar(val)
Bar((IEnumerable<T>)val)
(Compiles, but results in this error at run-time when tried with a List of ints:
Unable to cast object of type 'System.Collections.Generic.List`1[System.Int32]' to type 'System.Collections.Generic.IEnumerable`1[System.Collections.Generic.List`1[System.Int32]]'.'
Bar((IEnumerable)val)