Catch InvalidCastException properly in Collection (dotnet/corefx#39942)
authorJeffrey Zhao <jeffz@live.com>
Wed, 9 Oct 2019 20:19:34 +0000 (15:19 -0500)
committerSantiago Fernandez Madero <safern@microsoft.com>
Wed, 9 Oct 2019 20:19:34 +0000 (13:19 -0700)
commit822e291418de54e38bc5b2b55faf5ccf8df882cd
tree4bee9be3c402788ecb338611eb621b6269deebd2
parentf1f93232361a277f6a89d73492a71da1dfeb81c9
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
src/libraries/System.Runtime/tests/System/Collections/ObjectModel/CollectionTests.cs