Uploaded image for project: 'C# Driver'
  1. C# Driver
  2. CSHARP-4223

InvalidCastException for getting Serializer for a list/array of structs

    • Type: Icon: Bug Bug
    • Resolution: Gone away
    • Priority: Icon: Unknown Unknown
    • None
    • Affects Version/s: 2.15.1
    • Component/s: Serialization
    • None

      Summary

      I try to serialize/deserialize list/arrays of structs. I already implemented my own serializer/deserializer for structs. But lists or arrays of structs break already before serializing, when the BsonSerializerRegistry trys to get the serializer:

       

      InvalidCastException: Specified cast is not valid.
      MongoDB.Bson.Serialization.BsonSerializerRegistry.GetSerializer[T] () (at <8c9bb23734cb40cca67bc4a37718db2d>:0)
      MongoDB.Bson.Serialization.Serializers.EnumerableSerializerBase`2+<>c__DisplayClass4_0[TValue,TItem].<.ctor>b__0 () (at <8c9bb23734cb40cca67bc4a37718db2d>:0) 

       

      If I don't define any custom serializer, the serialization works somehow fine, but than the deserialization do not work.

      How to Reproduce

      public class MyDataClass {
        public object Id;
        public MyStruct[] Ms = MyStruct[2];
      }
      
      public struct MyStruct {
        public string SomeData;
      }
      
      ...
      MyDataClass data = new MyDataClass();
      BsonSerializer.Serialize(new MongoDB.Bson.IO.BsonDocumentWriter(new MongoDB.Bson.BsonDocument()), typeof(MyDataClass), data);
      ...

      Additional Background

      We write a part of framework for persisting game data to mongodb. Because the users of this framework likes to use structs for their data, because of the performance and so on, we have to support structs and also list/arrays of structs.

            Assignee:
            robert@mongodb.com Robert Stam
            Reporter:
            christian.zorneck@gmail.com Christian Zorneck
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: