[CSHARP-236] Can't map an user defined type (class or struct) to a bson value Created: 02/Jun/11 Updated: 02/Apr/15 Resolved: 02/Jun/11 |
|
| Status: | Closed |
| Project: | C# Driver |
| Component/s: | None |
| Affects Version/s: | 1.0 |
| Fix Version/s: | 1.1 |
| Type: | Bug | Priority: | Major - P3 |
| Reporter: | Silvio Massari | Assignee: | Robert Stam |
| Resolution: | Duplicate | Votes: | 0 |
| Labels: | bson, serialization, userDefined | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Environment: |
Windows 7 |
||
| Attachments: |
|
| Description |
|
In my project I'm using a user defined struct named Identity as id's type. So I implemented a BsonSerializer and an IIDGenerator. Then I registered them using the lines below: BsonSerializer.RegisterSerializer(typeof(Identity), new IdentitySerializer()); Then I defined an Order class the uses the Identity type: public class Order public decimal Amount { get; set; } } If I save a new Order into a collection, everything run well. So I did some debug and found that the exception was thrown by the method below: public static BsonValue MapToBsonValue(object value) { var message = string.Format(".NET type {0} cannot be mapped to a BsonValue", value.GetType().FullName); throw new ArgumentException(message); } I added some lines into the method to workaround the problem. The workaround tries to find a serializer for the value's type and if a serializer does exist, then it uses the serializer to convert the value to a valid BsonValue. public static BsonValue MapToBsonValue(object value) { BsonValue bsonValue; if (TryMapToBsonValue(value, out bsonValue)) { return bsonValue; } // --------------------------------------------------------- stream.Seek(0, SeekOrigin.Begin); using (var reader = BsonReader.Create(stream)) { reader.ReadStartDocument(); reader.ReadName(); return BsonValue.ReadFrom(reader); } } var message = string.Format(".NET type {0} cannot be mapped to a BsonValue", value.GetType().FullName); |
| Comments |
| Comment by Silvio Massari [ 02/Jun/11 ] |
|
Hi Robert. |
| Comment by Robert Stam [ 02/Jun/11 ] |
|
I believe this is the same issue as: https://jira.mongodb.org/browse/CSHARP-198 Can you test with the latest version of the master branch from github? |