[CSHARP-146] Enumerations cannot be serialized when part of a dictionary Created: 12/Jan/11  Updated: 02/Apr/15  Resolved: 14/Jan/11

Status: Closed
Project: C# Driver
Component/s: None
Affects Version/s: 1.0
Fix Version/s: 0.11

Type: Bug Priority: Major - P3
Reporter: Bailey Ling Assignee: Robert Stam
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

This occurs on the latest git source as of today (8d1d29365ebefea40d2c).

[Test]
public void Test()
{
var table = new Hashtable();
table["Text"] = "hello";
table["Enum"] = E.B;
new Doc

{ Values = table }

.ToBson();
}

The above test will fail with a MongoDB.Bson.BsonSerializationException : EnumSerializer cannot be used with type: System.Object.



 Comments   
Comment by Robert Stam [ 14/Jan/11 ]

This fix doesn't just apply to Hashtables with enum values; it applies whenever an enum is serialized with a nominalType of object. Since the enum is serialized as an int the type information is lost, so when this value is read back in it results in an instance of int, not of the original enum. This might be considered a bug in its own right, but would require writing type information to the serialized form to do anything different (and would not be cross platform).

Note that this is only a problem when the nominalType is object. When the nominalType is of the original enum type (as it is when deserializing an enum class property) the enum type is known (not because it is recorded in the serialized form, but because the nominalType specifies it).

Comment by Bailey Ling [ 12/Jan/11 ]

public class Doc
{
public Hashtable Values

{ get; set; }

}

Generated at Wed Feb 07 21:35:56 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.