C#下XML序列化出现System.InvalidOperationException的一种解释

Type.IsGenericParameter属性为false

当某个类的IsGenericParameter属性为false时,是表示该类为非泛型类或方法。

此时,如果该类的变量中或者方法的参数,包含泛型变量,则会产生该错误。

 

public class XXXX : MsgParent, IMsg
{
        public XXXX()
        {
            this.MessageType = "XXXX";
        }
        /// <summary>
        /// 确认队列
        /// </summary>
        public Queue<int> queue = new Queue<int>();

        /// <summary>
        /// 
        /// </summary>
        public int AlarmID = 0;

 

        public string Serialize()
        {

            string strSource = "";
            try
            {
                XmlSerializer s = new XmlSerializer(typeof(XXXXX));
                Stream stream = new MemoryStream();
                s.Serialize(stream, this);
                stream.Seek(0, SeekOrigin.Begin);
                using (StreamReader reader = new StreamReader(stream))
                {
                    strSource = reader.ReadToEnd();
                }
            }
            catch { }
            return strSource;
        }

        public static XXXXX Deserialize(string xmlSource)
        {
            XXXXX obj = new XXXXX();
            try
            {
                XmlSerializer x = new XmlSerializer(typeof(XXXXX));
                Stream stream = ProtocolHelper.GetStream(xmlSource);
                stream.Seek(0, SeekOrigin.Begin);
                obj = (XXXXX)x.Deserialize(stream);
                stream.Close();
            }
            catch
            { }
            return obj;
        }

    }

}

 

例如这样,就可能产生XML序列化出错。如果把Queue去掉的话,该问题又解决了。

但是这样又多出了许多麻烦。想要用到数据结构存储的话,就难免出现这样。如果是自己写一个数据结构(不用泛型),我想应该是可以的吧。

http://msdn.microsoft.com/zh-cn/library/system.type.isgenericparameter.aspx

MSDN上的参考资料。

发布了31 篇原创文章 · 获赞 21 · 访问量 20万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章