public void WriteXml(XmlWriter writer, object value, XmlSerializationContext context)
        {
            if (value == null)
            {
                return;
            }

            if (context.Member.MappingType != XmlMappingType.Element)
            {
                throw new XmlSerializationException($"XML mapping of \"{context.ValueType}\" must be Element.");
            }

            var target   = value;
            var contract = context.Contract.ToObjectContract();

            foreach (var property in contract.Properties)
            {
                if (CanWriteProperty(property))
                {
                    var propertyValue = GetValue(value, property);

                    if (!property.IsCollection)
                    {
                        context.Serialize(writer, propertyValue, property);
                    }
                    else
                    {
                        context.SerializeBody(writer, propertyValue, property);
                    }
                }
            }
        }
        public void WriteXml(XmlWriter writer, object value, XmlSerializationContext context)
        {
            var underlyingType = context.ValueType.GetUnderlyingOptionalType();
            var optional       = value as IAnyOptional;

            var underlyingValue = optional.HasValue ? optional.Value : underlyingType.GetDefault();

            context.SerializeBody(writer, underlyingValue, underlyingType);
        }
        public void WriteXml(XmlWriter writer, object value, XmlSerializationContext context)
        {
            var underlyingType = context.ValueType.GetUnderlyingNullableType();

            context.SerializeBody(writer, value, underlyingType);
        }