-3

I am new to serialization and am facing a problem. I have a serializable class, which MUST contain a method. But because of that method I am getting an error during initialization of serializer (when i comment the method, no errors are thrown). The question - is there a way to mark method in the class so serializer would ignore it and work properly?

This is my serializable class:

[Serializable]
    public class Key
    {
        [XmlAttribute("Id")]
        public Guid Id { get; set; }

        [XmlAttribute("Kid")]
        public Guid Kid { get; set; }

        [XmlElement("CEK")]
        public string CEK { private get; set; }

        public string Foo()
        {...}
    }

I have a little modified it in terms of privacy of data, but it doesn't influence the topic.

Here is how I do the serialization:

        StringReader strReader = null;
            XmlSerializer serializer = null;
            XmlTextReader xmlReader = null;
            Object obj = null;
            try
            {
                strReader = new StringReader(xml);
                serializer = new XmlSerializer(objectType,
                    new XmlRootAttribute
                    {
                        ElementName = "someNS",
                        Namespace = "url.to.some.namespace"
                    });
                xmlReader = new XmlTextReader(strReader);
                obj = serializer.Deserialize(xmlReader);
         }

Error is thrown at this part:

serializer = new XmlSerializer(objectType,
    new XmlRootAttribute
    {
        ElementName = "someNS",
        Namespace = "url.to.some.namespace"
    });

UPDATE: Right, forgot about the error. It is the following:

System.InvalidOperationException:'There was an error reflecting type 'MyProject.Objects.Key'.'

and inner exeptions:

InvalidOperationException: There was an error reflecting property 'Key'. InvalidOperationException: There was an error reflecting type 'MyProject.Objects.Key'.

  • 1
    You forgot to tell us what the error is. – JLRishe Oct 25 '17 at 18:04
  • 1
    Please check [MCVE] guidance on providing code... Serializes generally ignore methods, so without actual error message or complete code to reproduce the error it is hard to guess what is wrong. – Alexei Levenkov Oct 25 '17 at 18:09
  • And `[Serializable]` is not needed unless you want to use `BinaryFormatter` for serialization. – György Kőszeg Oct 25 '17 at 18:10
  • Are there no inner exceptions within those? [This answer](https://stackoverflow.com/a/60581/1945651) suggests that "There was an error reflecting type" errors have an inner exception indicating the root cause. – JLRishe Oct 25 '17 at 18:14

1 Answers1

1

Fixed it :

[XmlElement("CEK")]
public string CEK { private get; set; }

This property caused exception; you can't make get a private method in serializable class.