Esempio n. 1
0
        public static T DeserializeObject <T>(string xml)
        {
            xml.AssertHasText("xml");

            StringReader  strReader  = null;
            XmlSerializer serializer = null;
            XmlTextReader xmlReader  = null;
            T             returnObj  = default(T);

            try
            {
                object obj = null;
                strReader  = new StringReader(xml);
                serializer = new XmlSerializer(typeof(T));
                xmlReader  = new XmlTextReader(strReader);
                obj        = serializer.Deserialize(xmlReader);

                if (obj is T)
                {
                    returnObj = (T)obj;
                }
                else
                {
                    returnObj = (T)Convert.ChangeType(obj, typeof(T));
                }
            }
            catch (InvalidCastException)
            {
                return(returnObj);
            }
            finally
            {
                if (xmlReader.IsNotNull())
                {
                    xmlReader.Close();
                }
                if (strReader.IsNotNull())
                {
                    strReader.Close();
                }
            }

            return(returnObj);
        }