对于程序命名空间修改,原序列化的二进制对象的处理

·

1 min read

问题: 现有一程序框架,因需要要变更命名空间.可变更后不能使用? 原因: 检查发现是由于原命名空间下序列化写入了一个二进制对象.现命名空间变更后不能获取,引发了异常. 处理: (1)用原命名空间下,读取写入的对象数据.并记录. (2)用现在的命名空间下实例化相应的对象,并记录数据赋值写入. (3)将对象重新序列化并写入二进制对象中. 经过处理问题问题解决! 关键代码:

  protected void BtnWrite\_Click(object sender, EventArgs e)

        {

            string ConnString = string.Format("Provider=Microsoft.Jet.OleDb.4.0;Data Source={0}{1};Persist Security Info=True;", AppDomain.CurrentDomain.BaseDirectory, Commons.GetConnString);

            using (OleDbConnection conn = new OleDbConnection(ConnString))

            {

                OleDbCommand cmd = new OleDbCommand("update  sys\_SystemInfo set \[email=ConfigData=@ConfigData\]SConfigData=@S\_SystemConfigData\[/email\] where SystemID=2");

                Websharp.AptitudeFramework.FrameWork.Components.ConfigDataTable sc = new Websharp.AptitudeFramework.FrameWork.Components.ConfigDataTable();



                byte\[\] by = Scf(sc);

                cmd.Parameters.Add("@ConfigData", OleDbType.VarBinary).Value = by;

                conn.Open();

                cmd.Connection = conn;

                cmd.ExecuteNonQuery();



            }

        }



        /// <summary>

        /// 序列化ConfigDataTable类

        /// </summary>

        /// <param name="it">ConfigDataTable类</param>

        /// <returns>byte\[\]字节流</returns>

        public byte\[\] Scf(ConfigDataTable it)

        {

            IFormatter formatter = new BinaryFormatter();

            MemoryStream ms = new MemoryStream();

            byte\[\] b;

            formatter.Serialize(ms, it);

            ms.Position = 0;

            b = new byte\[ms.Length\];

            ms.Read(b, 0, b.Length);

            ms.Close();

            return b;

        }