简体   繁体   中英

.net 4.6 Methods in Serializable class

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'.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM