由于序列化的时候错误难以详细报出.所以这里收集一下大家认为对对象进行序列化的时候的注意事项.比如我先说我知道的几个1 .不要用接口定义属性类型
2 .属性类型必须都可序列化希望大家补充修正。

解决方案 »

  1.   

    我也来说一句..类似Button之类的类型不能被序列化,或者说不能直接被序列化..
      

  2.   

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Data;namespace Luna.Base
    {
        [Serializable]
        public abstract class AbstractComponent : ILunaComponent
        {
            [NonSerialized]
            private LunaPropertyCollection propertyCollection;
            private List<AbstractComponent> children;
            private ComponentState state;        public AbstractComponent()
            {
                propertyCollection = new LunaPropertyCollection(null);
                state = ComponentState.Unchanged;
            }
            
            #region ILunaComponent 成员        public string ID
            {
                get
                {
                    return GetProoertyValue("ID");
                    //if (propertyCollection["ID"] == null)
                    //    return null;
                    //return propertyCollection["ID"].ToString();
                }
                set { SetProoertyValue("ID", value); }
            }
            public string Code
            {
                get
                {
                    return GetProoertyValue("Name");
                    //if (propertyCollection["Code"] == null)
                    //    return null;
                    //return propertyCollection["Code"].Value.ToString();
                }
                set
                {
                    SetProoertyValue("Code", value);
                }
            }        public string Name
            {
                get
                {
                    return GetProoertyValue("Name");
                    //if (propertyCollection["Name"] == null)
                    //    return null;
                    //return propertyCollection["Name"].Value.ToString();
                }
                set
                {
                    SetProoertyValue("Name", value);
                }
            }        public virtual string Table
            {
                get
                {
                    return GetProoertyValue("LUNAOBJECT");
                    //if (propertyCollection["LUNAOBJECT"] == null)
                    //    return null;
                    //return Properties["LUNAOBJECT"].Value;
                }
            }        public string Description
            {
                get
                {
                    return GetProoertyValue("Description");
                    //return propertyCollection["Description"].Value.ToString();
                }
                set
                {
                    SetProoertyValue("Description", value);
                }
            }        public string ComponentClass
            {
                get
                {
                    return GetProoertyValue("ComponentClass");
                    //if (propertyCollection["ComponentClass"] == null)
                    //    return null;
                    //return propertyCollection["ComponentClass"].Value.ToString();
                }
            }        public DateTime CreateTime
            {
                get
                {
                    if (propertyCollection["CreateTime"] == null || propertyCollection["CreateTime"].ToString().Trim() == "")
                        return DateTime.MinValue;
                    return ConvertUtil.GetDateTime(propertyCollection["CreateTime"].Value);
                }
                set
                {
                    SetProoertyValue("CreateTime", ConvertUtil.GetString(value));
                }
            }        public DateTime UpdateTime
            {
                get
                {
                    if (propertyCollection["UpdateTime"] == null || propertyCollection["UpdateTime"].ToString().Trim() == "")
                        return DateTime.MinValue;
                    return ConvertUtil.GetDateTime(propertyCollection["UpdateTime"].Value);
                }
                set
                {
                    SetProoertyValue("UpdateTime", ConvertUtil.GetString(value));
                }
            }        public LunaPropertyCollection Properties
            {
                get { return propertyCollection; }
            }               protected void SetProoertyValue(string key,string value)
            {
                if (propertyCollection[key] == null)
                {
                    this.propertyCollection.Add(key, value);
                }
                propertyCollection[key].Value = value;
            }        protected string GetProoertyValue(string key)
            {
                if (propertyCollection[key] == null)
                {
                    return null;
                }
                else
                {
                    return propertyCollection[key].Value;
                }
            }        /// <summary>
            /// 子集合
            /// </summary>
            public List<AbstractComponent> Children
            {
                get
                {
                    if (children == null)
                        children = new List<AbstractComponent>();
                    return children;
                }
                set
                {
                    children = value;
                }
            }        public ComponentState State
            {
                get
                {
                    return state;
                }
                set
                {
                    state = value;
                }
            }        #endregion
            
        }
    }当这个类的子类的对象实例被序列化时会提示xml声称错误。(只有在obj.Children.Count>0的时候会报错)请问为什么?