// public methods /// <summary> /// Applies a modification to the member map. /// </summary> /// <param name="memberMap">The member map.</param> public void Apply(BsonMemberMap memberMap) { if (memberMap.MemberType == _type) { memberMap.SetDefaultValue(_defaultValue); } }
// public methods /// <summary> /// Applies a modification to the member map. /// </summary> /// <param name="memberMap">The member map.</param> public void Apply(BsonMemberMap memberMap) { if (memberMap.MemberType == _type) { memberMap.SetDefaultValue(_defaultValue); } }
public void Apply(BsonMemberMap memberMap) { if (memberMap.MemberType == typeof(Guid)) { memberMap.SetDefaultValue(Guid.Empty); } }
public void Apply(BsonMemberMap memberMap) { if (!typeof(IEnumerable).IsAssignableFrom(memberMap.MemberType) || //Allow IEnumerable typeof(string) == memberMap.MemberType || //But not String typeof(IDictionary).IsAssignableFrom(memberMap.MemberType)) //Or Dictionary (concrete classes only see below) { return; } //*NOTE Microsoft was too stupid to make the generic dictionary interfaces implement IDictonary even though every single concrete class does // They were also too stupid to make generic IDictionary implement IReadOnlyDictionary even though every single concrete class does I believe this should catch all if (memberMap.MemberType.IsGenericType && memberMap.MemberType.IsInterface) { var genericType = memberMap.MemberType.GetGenericTypeDefinition(); if (genericType == typeof(IDictionary <,>) || genericType == typeof(IReadOnlyDictionary <,>)) { return; } } if (memberMap.MemberType.IsArray) //Load Empty Array { memberMap.SetDefaultValue(() => Array.CreateInstance(memberMap.MemberType.GetElementType(), 0)); } else if (!memberMap.MemberType.IsInterface) //Create ConcreteType directly { memberMap.SetDefaultValue(() => Activator.CreateInstance(memberMap.MemberType)); } else if (memberMap.MemberType.IsGenericType) //Generic Interface type { var interfaceType = memberMap.MemberType.GetGenericTypeDefinition(); var concreteType = InterfaceToConcreteMap.ContainsKey(interfaceType) ? InterfaceToConcreteMap[interfaceType] : DefaultType; memberMap.SetDefaultValue(() => Activator.CreateInstance(concreteType.MakeGenericType(memberMap.MemberType.GetGenericArguments()))); } else //This should just be the antique non generic interfaces like ICollection, IEnumerable, etc. { memberMap.SetDefaultValue(() => Activator.CreateInstance(typeof(List <object>))); } memberMap.SetShouldSerializeMethod(instance => { var value = (IEnumerable)memberMap.Getter(instance); return(value?.GetEnumerator().MoveNext() ?? false); }); }
// public methods /// <summary> /// Applies a modification to the member map. /// </summary> /// <param name="memberMap">The member map.</param> public void Apply(BsonMemberMap memberMap) { memberMap.SetDefaultValue(_defaultValue); if (_serializeDefaultValueWasSet) { memberMap.SetIgnoreIfNull(false); memberMap.SetIgnoreIfDefault(!_serializeDefaultValue); } }
// public methods /// <summary> /// Applies a modification to the member map. /// </summary> /// <param name="memberMap">The member map.</param> public void Apply(BsonMemberMap memberMap) { memberMap.SetDefaultValue(_defaultValue); if (_serializeDefaultValueWasSet) { memberMap.SetIgnoreIfNull(false); memberMap.SetIgnoreIfDefault(!_serializeDefaultValue); } }
public void Apply(BsonClassMap classMap) { var properties = classMap.ClassType .GetTypeInfo() .GetProperties(_bindingFlags) .Where(p => p.PropertyType != typeof(Type)) .ToList(); var mappingProperties = properties .Where(p => IsReadOnlyProperty(classMap, p) || IsInitOnlyProperty(p)) .ToList(); foreach (PropertyInfo property in mappingProperties) { BsonMemberMap member = classMap.MapMember(property); if (IsNullableProperty(property)) { member.SetDefaultValue((object?)null); } } if (!classMap.ClassType.IsAbstract) { foreach (ConstructorInfo constructor in classMap.ClassType.GetConstructors( BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public)) { List <PropertyInfo> matchProperties = GetMatchingProperties(constructor, properties); if (matchProperties.Any()) { BsonCreatorMap creatorMap = classMap.MapConstructor(constructor); creatorMap.SetArguments(matchProperties); } } } }