Catch InvalidCastException properly in Collection (dotnet/corefx#39942)
The explicit implementations of IList.Add/Insert/Set methods catch
InvalidCastException for casting input argument to type T to throw
an ArgumentException. The issue here is they put the core collection
modification operation in the try block, and it could be overridden
by sub-classes and produce an InvalidCastException from user logic,
even the argument passed to IList.Add/Insert/Set is valid.
This commit contains the test cases for those three method. In those
test cases, we create an ObservableCollection (which is the sub-class
of Collection) and throw an InvalidCastException from the handler of
CollectionChanged event. This exception should be observed from the
outside when we pass a valid input, instead of an ArgumentException
as if we've passed an argument with invalid type.
Fix dotnet/corefx#39919
Commit migrated from https://github.com/dotnet/corefx/commit/
fe8db47cb290eff654e8419ce6d615e9bb2ce189