[CSHARP-4223] InvalidCastException for getting Serializer for a list/array of structs Created: 21/Jun/22 Updated: 27/Oct/23 Resolved: 11/Jul/22 |
|
| Status: | Closed |
| Project: | C# Driver |
| Component/s: | Serialization |
| Affects Version/s: | 2.15.1 |
| Fix Version/s: | None |
| Type: | Bug | Priority: | Unknown |
| Reporter: | Christian Zorneck | Assignee: | Robert Stam |
| Resolution: | Gone away | Votes: | 0 |
| Labels: | None | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Description |
| Comments |
| Comment by PM Bot [ 11/Jul/22 ] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
There hasn't been any recent activity on this ticket, so we're resolving it. Thanks for reaching out! Please feel free to comment on this if you're able to provide more information. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Robert Stam [ 23/Jun/22 ] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
All serializers must implement `IBsonSerializer<T>`. It is not sufficient to only implement `IBsonSerializer`. The non-generic `IBsonSerializer` interface is only intended to be used to pass around serializers when `<T>` is not known at compile time, but the actual serializer must still implement `IBsonSerializer<T>`. Even if a custom serializer implementing only `IBsonSerializer` appears to work in limited scenarios, that is accidental, not by design.
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Christian Zorneck [ 22/Jun/22 ] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@Robert thank you for your fast investigation! After days of trying and yesterday opening this issue, I finally found the problem. I used the non generic interface for my serializer:
...but you have to use the generic variant of this interface, to work with lists:
Without using this, it works fine if the field or property is of the type of the struct, but throws the InvalidCastException, if it is part of a list or array. Than the driver trys to cast it to the generic interface, which do not work.
I don't know if this should/can be handled in some way, that it do not throw this exception, and cast and use it without generic type, like it is doing if it is not part of list/array. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Robert Stam [ 21/Jun/22 ] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
I am unable to reproduce this. I used the following test to attempt to reproduce. I had to fill in some missing pieces to get a a test that would compile and run. You could compare the pieces I wrote to your own code to see if I did anything different than you.
|