简体   繁体   中英

Genereic de-serialization method in C#

I have this method to Serialize an object with any type

    public static string SerializeObject<T>(this T toSerialize)
    {
        XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
        StringWriter textWriter = new StringWriter();

        xmlSerializer.Serialize(textWriter, toSerialize);
        return textWriter.ToString();
    }

Now I need some method to deserialize string into object type which I can cast depends on some code.

My question is how this method could be implemented?

Thank you guys!

public static T Deserialize<T>(string xmlDataToDeSerialize)
{
    XmlSerializer xmlDeSerializer = new XmlSerializer(typeof(T));
    StringReader stringReader = new StringReader(xmlDataToeSerialize);
    return (T)xmlDeSerializer.Deserialize(stringReader);            
}

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