MongoDB.Bson.Serialization.Conventions.ConventionProfile.Merge C# (CSharp) Method

Merge() public method

Merges another convention profile into this one (only missing conventions are merged).
public Merge ( ConventionProfile other ) : void
other ConventionProfile The other convention profile.
return void
        public void Merge(ConventionProfile other)
        {
            if (DefaultValueConvention == null)
            {
                DefaultValueConvention = other.DefaultValueConvention;
            }
            if (ElementNameConvention == null)
            {
                ElementNameConvention = other.ElementNameConvention;
            }
            if (ExtraElementsMemberConvention == null)
            {
                ExtraElementsMemberConvention = other.ExtraElementsMemberConvention;
            }
            if (IdGeneratorConvention == null)
            {
                IdGeneratorConvention = other.IdGeneratorConvention;
            }
            if (IdMemberConvention == null)
            {
                IdMemberConvention = other.IdMemberConvention;
            }
            if (IgnoreExtraElementsConvention == null)
            {
                IgnoreExtraElementsConvention = other.IgnoreExtraElementsConvention;
            }
#pragma warning disable 618 // SerializeDefaultValueConvention is obsolete
            if (IgnoreIfDefaultConvention == null && SerializeDefaultValueConvention == null)
            {
                if (other.SerializeDefaultValueConvention != null)
                {
                    SerializeDefaultValueConvention = other.SerializeDefaultValueConvention;
                }
                else
                {
                    IgnoreIfDefaultConvention = other.IgnoreIfDefaultConvention;
                }
            }
#pragma warning restore 618
            if (IgnoreIfNullConvention == null)
            {
                IgnoreIfNullConvention = other.IgnoreIfNullConvention;
            }
            if (MemberFinderConvention == null)
            {
                MemberFinderConvention = other.MemberFinderConvention;
            }
            if (SerializationOptionsConvention == null)
            {
                SerializationOptionsConvention = other.SerializationOptionsConvention;
            }
        }

Usage Example

Ejemplo n.º 1
0
        /// <summary>
        /// Registers the MongoDB Bson serialization conventions.
        /// </summary>
        /// <param name="autoGenerateID">A <see cref="Boolean"/> value which indicates whether
        /// the ID value should be automatically generated when a new document is inserting.</param>
        /// <param name="localDateTime">A <see cref="Boolean"/> value which indicates whether
        /// the local date/time should be used when serializing/deserializing <see cref="DateTime"/> values.</param>
        /// <param name="additionConventions">Additional conventions that needs to be registered.</param>
        public static void RegisterConventions(bool autoGenerateID, bool localDateTime, ConventionProfile additionConventions)
        {
            var convention = new ConventionProfile();
            convention.SetIdMemberConvention(new NamedIdMemberConvention("id", "Id", "ID", "iD"));

            if (autoGenerateID)
                convention.SetIdGeneratorConvention(new GuidIDGeneratorConvention());

            if (localDateTime)
                convention.SetSerializationOptionsConvention(new UseLocalDateTimeConvention());

            if (additionConventions != null)
                convention.Merge(additionConventions);

            BsonClassMap.RegisterConventions(convention, type => true);
        }