locked
Cannot serialize object into xml because System.ComponentModel.PropertyChangedEventHandler RRS feed

  • Question

  • User-1952516322 posted

    Hello,

    I have a WCF request, what I want is to log the object before send it to service, this object has below field

    [System.Diagnostics.DebuggerStepThroughAttribute()]
        [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "4.0.0.0")]
        [System.Runtime.Serialization.DataContractAttribute(Name="xxx", Namespace="xxxx")]
        [System.SerializableAttribute()]
        public partial class abc : object, System.Runtime.Serialization.IExtensibleDataObject, System.ComponentModel.INotifyPropertyChanged {
            
            [System.NonSerializedAttribute()]
            private System.Runtime.Serialization.ExtensionDataObject extensionDataField;
    
            private System.ComponentModel.PropertyChangedEventHandler PropertyChanged1Field;
            
    
    [global::System.ComponentModel.BrowsableAttribute(false)]
            public System.Runtime.Serialization.ExtensionDataObject ExtensionData {
                get {
                    return this.extensionDataField;
                }
                set {
                    this.extensionDataField = value;
                }
            }
            
            [System.Runtime.Serialization.DataMemberAttribute(Name="PropertyChanged", IsRequired=true)]
            public System.ComponentModel.PropertyChangedEventHandler PropertyChanged1 {
                get {
                    return this.PropertyChanged1Field;
                }
                set {
                    if ((object.ReferenceEquals(this.PropertyChanged1Field, value) != true)) {
                        this.PropertyChanged1Field = value;
                        this.RaisePropertyChanged("PropertyChanged1");
                    }
                }
            }
    }

    I have this function to log the object and serialize it

    public static void LogObject<T>(T value)
            {
                
                try
                {
                    var xmlserializer = new XmlSerializer(typeof(T));   // << In this line exception shown.
                    var stringWriter = new StringWriter();
                    using (var writer = XmlWriter.Create(stringWriter))
                    {
                        xmlserializer.Serialize(writer, value);
                        var message = stringWriter.ToString();
    
                        if (_logger.IsInfoEnabled)
                            _logger.Info(message);
    
    
                    }
                }
                catch (Exception ex)
                {
                }
            }

    When I log the object by this code Logging.LogObject<abc>(objABC);

    then I found this exception

    Please Help!

    Exception= {"Cannot serialize member 'xxx.abc.PropertyChanged1' of type 'System.ComponentModel.PropertyChangedEventHandler', see inner exception for more details."}
    
    Message = "System.ComponentModel.PropertyChangedEventHandler cannot be serialized because it does not have a parameterless constructor."

    Note: I tried to add [NonSerialized] attribute above the field, but issue not solved

    Friday, September 13, 2019 10:11 PM

Answers

  • User665608656 posted

    Hi Khalid,

    Best regards Yongping for your reply,  but this class coming from service, I didn't create it.

    If so, I suggest you create a new class, copy the contents of the abc class into the new class, and then add a constructor to the new class.

    Best Regards,

    YongQing,

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, September 23, 2019 9:44 AM

All replies

  • User665608656 posted

    Hi Khalid,

    According to your error message, it indicates that in order to serialize or deserialize a class, a serializer needs a parametric constructor, and you need to add a parametric constructor to your class.

    For more details, you could refer to this link : https://stackoverflow.com/a/10672267

    Best Regards,

    YongQing.

    Monday, September 16, 2019 9:27 AM
  • User-1952516322 posted

    Best regards Yongping for your reply,  but this class coming from service, I didn't create it.

    Sunday, September 22, 2019 11:04 AM
  • User665608656 posted

    Hi Khalid,

    Best regards Yongping for your reply,  but this class coming from service, I didn't create it.

    If so, I suggest you create a new class, copy the contents of the abc class into the new class, and then add a constructor to the new class.

    Best Regards,

    YongQing,

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, September 23, 2019 9:44 AM