XmlSerializer, “Specified” suffix and IReflect

后端 未结 3 1846
清酒与你
清酒与你 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:31

    Code sample as an extension to the provided answer;

    WSDL:

    
        
          
          
          
        
      
    
        
          
          
        
      
    

    Generated .net SOAP client class

    System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.7.3056.0")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(Namespace="your namespace")]
    public partial class advancedSearchRequest : object, System.ComponentModel.INotifyPropertyChanged {
    
        private vehicleType vehicleTypeField;
    
        private bool vehicleTypeFieldSpecified;
    
        private string searchField;
    
    
        /// 
        [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified, Order=0)]
        public vehicleType vehicleType {
            get {
                return this.vehicleTypeField;
            }
            set {
                this.vehicleTypeField = value;
                this.RaisePropertyChanged("vehicleType");
            }
        }
    
        /// 
        [System.Xml.Serialization.XmlIgnoreAttribute()]
        public bool vehicleTypeSpecified {
            get {
                return this.vehicleTypeFieldSpecified;
            }
            set {
                this.vehicleTypeFieldSpecified = value;
                this.RaisePropertyChanged("vehicleTypeSpecified");
            }
        }
    
        [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified, Order=3)]
        public string search {
            get {
                return this.searchField;
            }
            set {
                this.searchField = value;
                this.RaisePropertyChanged("search");
            }
        }
    
    }
    

    You can set "vehicleTypeFieldSpecified" = {true/false} to {serialize/omit} it;

    advancedSearchRequest.vehicleTypeField = vehicleType.BIKE;
    advancedSearchRequest.vehicleTypeFieldSpecified = true;
    

    Resulting SOAP message;

    
     BIKE
     abc
    
    

提交回复
热议问题