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

MongoDB C# Driver (2.7) and serialization issues

    XMLWordPrintable

    Details

    • Type: Task
    • Status: Open
    • Priority: Minor - P4
    • Resolution: Unresolved
    • Affects Version/s: 2.7.0
    • Fix Version/s: None
    • Component/s: Serialization
    • Labels:
    • Environment:
      .net framework 4.5

      Description

      The following post is more or less a copy-paste of a question I posted on stackoverflow.

      My colleagues suggested me to also post a question here on JIRA, so here I am.

      I also provided a minimalistic sample code to reproduce the error.

      Link to SO: https://stackoverflow.com/questions/51285902/mongodb-c-sharp-driver-2-7-and-serialization-issues

      ===== ISSUE AND QUESTION ====

      I'm currently having a strange issue with the latest mongodb c# driver (2.7), inheritance and serialization.I'm currently having a strange issue with the latest mongodb c# driver (2.7), inheritance and serialization.

      Here is a super simplified class hierarchy that I'm using:

      public abstract class BaseClass { 
        public Guid Id { get; private set; }
        protected BaseClass(Guid id) { Id = id; } 
      }     
       
      public class DerivedClass : BaseClass 
      { 
        public DerivedClass(Guid id) : base(id) {} 
      }
      

      When I try to register the class for mongo serialization, I am getting an error:

      var type = typeof(DerivedClass);
      if (BsonClassMap.IsClassMapRegistered(type)) 
        return;
      var cm = new BsonClassMap(type); cm.AutoMap();
      BsonClassMap.RegisterClassMap(cm);
      

      The error is:

      The memberInfo argument must be for class DerivedClass, but was for class BaseClass.    Parameter name: memberInfo
      

      Now, and this is where things start to get weird, when I tried to understand what was happening I made some changes to the BaseClass or the DerivedClass.

      ANY of the two changes listed below will make the `RegisterClassMap` method work without errors...

      change 1: adding a second, unused property on BaseClass

      public abstract class BaseClass { 
        public Guid Id { get; private set; } 
        public string Test { get; }
        protected BaseClass(Guid id) { Id = id; } 
      }
      

      change 2: changing the Id property to have a non-private setter (any other works)

      public abstract class BaseClass { 
        public Guid Id { get; protected set; }
        protected BaseClass(Guid id) { Id = id; } 
      } 
      

      THE QUESTION
      My question is... what is happening? While I may agree that for the serializer you should have an accessible setter for every property on the class that you want to map (in this case, DerivedClass), why the case #1 is actually not giving any error?

       

        Attachments

          Activity

            People

            • Votes:
              0 Vote for this issue
              Watchers:
              5 Start watching this issue

              Dates

              • Created:
                Updated: