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

Impossible to create BsonClassMap for Immutable Type that Inherits

    • Type: Icon: Bug Bug
    • Resolution: Duplicate
    • Priority: Icon: Minor - P4 Minor - P4
    • None
    • Affects Version/s: 2.11.0
    • Component/s: BSON
    • Labels:
      None

      See the function I have that creates a class map for my immutable types below.

      I am getting the error

      The memberInfo argument must be for class {0}, but was for class {1}

      from EnsureMemberInfoIsForThisClass when I try to map an immutable inheriting type. (Employee in my example.) I think that maybe there should be a new function in BsonClassMap which maps a member to a constructor argument. Inside that function, it should use a different version of EnsureMemberInfoIsForThisClass that checks

      if (memberInfo.ReflectedType == _classType)

      rather than

      if (memberInfo.DeclaringType == _classType)

      . Unless I am missing something here.

      I am working on implementing this on my fork and will submit a PR once done.

              private static void MapClass<T>(BsonClassMap<T> map)
              {
                  var type = typeof(T);
                  map.AutoMap();
      
                  // the SelectConstructor function finds the right constructor which can be used for mapping. 
                  var ctor = SelectConstructor(type);
                  if (ctor != null)
                  {
                      map.MapConstructor(ctor.Info)
                          .SetArguments(ctor.Members);
                      ctor.Members.Run(memberInfo => {
                          if(memberInfo.ReflectedType != memberInfo.DeclaringType)
                          {
                              // What to do??
                              // Say we have: 
                              // public class Person { 
                              //   public Person(string name) {
                              //     Name = name;
                              //   }
                              //   public string Name { get; } 
                              // } 
      
                              // and 
      
                              // class Employee : Person { 
                              //   public Employee(string name, string title) : base(name) {
                              //     Title = title;
                              //   }
                              //   string Title { get; } 
                              // }
      
                              // then we have BsonClassMap<Employee>.MapConstructor() for the Employee(string, string) ctor.
                              // Then we also have to BsonClassMap<Employee>.MapMember() for Person.Name 
                              // so that it will be passed to the constructor (at least that is my understanding of whats going on)
      
                          } else {
                              map.MapMember(memberInfo);
                          }
                      });
                  }
      
                  var idProperty = type.GetProperties().SingleOrDefault(x => x.IsDefined(typeof(QueryIdAttribute)));
                  if (idProperty != null)
                      map.MapIdMember(idProperty);
              }
      

            Assignee:
            Unassigned Unassigned
            Reporter:
            forestjohnson Forest Johnson
            Votes:
            4 Vote for this issue
            Watchers:
            6 Start watching this issue

              Created:
              Updated:
              Resolved: