XmlSerializer, “Specified” suffix and IReflect

后端 未结 3 1825
清酒与你
清酒与你 2021-01-02 15:06

I\'ve discovered that if a serializable Field/Property has a corresponding field of type Boolean having as a name the Field/Property name with \"S

3条回答
  •  伪装坚强ぢ
    2021-01-02 15:28

    I will extend answer of Martin Peck. You can avoid serialization of the fields/properties with "Specified" suffix. You should define that "*Specified" properties in your class and apply [XmlIgnoreAttribute()] to them.

    Here is an example:

    [System.SerializableAttribute()]
    [System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://yournamespace.com")]
    public partial class YourObject
    {
        private long sessionTimeoutInSecondsField;
    
        private bool sessionTimeoutInSecondsFieldSpecified;
    
        public long sessionTimeoutInSeconds
        {
            get
            {
                return this.sessionTimeoutInSecondsField;
            }
            set
            {
                this.sessionTimeoutInSecondsField = value;
            }
        }
    
        [System.Xml.Serialization.XmlIgnoreAttribute()]
        public bool sessionTimeoutInSecondsSpecified
        {
            get
            {
                return this.sessionTimeoutInSecondsFieldSpecified;
            }
            set
            {
                this.sessionTimeoutInSecondsFieldSpecified = value;
            }
        }
    }
    

提交回复
热议问题