服务器可以成功的反序列化客户端发过来的User对象,但是反序列化ScanfAppTaskInfo对象时提示:输入流是无效的二进制格式,其 中User对象和ScanfAppTaskInfo对象定义在了同一个ClassLibrary中,并且生成的ClassLibrary.dll文件客户 端和服务器同时都引用了,定义User类和ScanfAppTaskInfo类的时候也都同时加上了[Serializable],序列化和反序列化 User对象和ScanfAppTaskInfo对象用的是相同的方法,那反序列化ScanfAppTaskInfo对象时出错会是什么原因呢?下面是分 别是序列化和反序列化User对象和ScanfAppTaskInfo对象的代码
User类的定义
namespace ClassLibrary
{
    [Serializable]
    public class User
    {
        private string uname = "";
        private string name = "";
        private string pwd = "";
        private string department = "";
        //private Role role = null;        public string Username
        {
            get { return uname; }
            set { uname = value; }
        }        public string Name
        {
            get { return name; }
            set { name = value; }
        }        public string Password
        {
            get { return pwd; }
            set { pwd = value; }
        }
       
        //public Role Role
        //{
        //    get { return role; }
        //    set { role = value; }
        //}        public string Department
        {
            get { return department; }
            set { department = value; }
        }
    }
}
ScanfAppTaskInfo类的定义
namespace ClassLibrary
{
    [Serializable]
    public class ScanfAppTaskInfo
{
       //private User user=null;
       private string use = "";
       private string des="";
       private string cls="";
       private string filename="";
       private string scanfRes="";
       private string appOpn="";
       private int pages=0;
       private int counts=0;
       private DataTable dtper = null;       //public User User
       //{
       //    get { return user; }
       //    set { user = value; }
       //}
       public string Use
       {
           get { return use; }
           set { use = value; }
       }
       public string Des
       {
           get { return des; }
           set { des = value; }
       }
       public string Cls
       {
           get { return cls; }
           set { cls = value; }
       }
       public string Filename
       {
           get { return filename; }
           set { filename = value; }
       }
       public string ScanfRes
       {
           get { return scanfRes; }
           set { scanfRes = value; }
       }
       public string AppOpn
       {
           get { return appOpn; }
           set { appOpn = value; }
       }
       public int Pages
       {
           get { return pages; }
           set { pages = value; }
       }
       public int Counts
       {
           get { return counts; }
           set { counts = value; }
       }       public DataTable Dtperson
       {
           get { return dtper; }
           set { dtper = value; }
       } }
}
 
初始化User对象并序列化发给服务器端
user.Username = tbName.Text.Trim();
            user.Password = tbPwd.Text.Trim();          
            //contactWithServer.User = user;
            try
            {
                byte[] command = Encoding.ASCII.GetBytes("Login");  //命令
                byte[] cmd = new byte[20];
                for (int i = 0; i < command.Length; i++)
                {
                    cmd[i]=command[i];
                }
                memStream = new MemoryStream();
                formatter.Serialize(memStream, user);
                byte[] data = memStream.ToArray();                  //将User对象发送给服务器
                memStream.Flush();
                byte[] buffer = new byte[cmd.Length + data.Length];
                Array.Copy(cmd,0,buffer,0,cmd.Length);
                Array.Copy(data,0,buffer,cmd.Length,data.Length);
                contactWithServer.sendMsg(buffer,buffer.Length);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                MessageBox.Show(ex.StackTrace);
            }
            finally
            {
                memStream.Close();
            }
服务器端反序列化User对象
[code=csharp]buff = new byte[data.Length - leng];
                    for (int j = 0; j < data.Length - leng; j++)
                        buff[j] = data[leng + j];
                    memStream = new MemoryStream(buff);
                    memStream.Seek(0, SeekOrigin.Begin);
                    obj = formatter.Deserialize(memStream);
                    memStream.Close();
                    if (obj != null && obj is User)
                        user = (User)obj;
客户端初始化ScanfAppTaskInfo对象并序列化并发给服务器
 sataskinfo.Use = cbusing.Text.Trim();
            sataskinfo.Des = cbdestination.Text.Trim();
            sataskinfo.Cls = cbclassfied.Text.Trim();
            sataskinfo.Pages = (int)numericpages.Value;
            sataskinfo.Counts = (int)numericcount.Value;
            sataskinfo.Filename = tbfilename.Text.Trim();
            sataskinfo.ScanfRes = tbScanreason.Text.Trim();            try
            {
                byte[] command = Encoding.ASCII.GetBytes("CmfScanfApp");  //命令
                byte[] cmd = new byte[20];
                for (int i = 0; i < command.Length; i++)
                {
                    cmd[i] = command[i];
                }
                memStream = new MemoryStream();
                formatter.Serialize(memStream, sataskinfo);
                byte[] data = memStream.ToArray();                  //将User对象发送给服务器
                memStream.Flush();
                byte[] buffer = new byte[cmd.Length + data.Length];
                Array.Copy(cmd, 0, buffer, 0, cmd.Length);
                Array.Copy(data, 0, buffer, cmd.Length, data.Length);
                MessageBox.Show(buffer.Length.ToString(),"djkkkkkkkkkkk");
                contanctWithServer.sendMsg(buffer, buffer.Length);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                MessageBox.Show(ex.StackTrace);
            }
            finally
            {
                memStream.Close();
            }[/code]
服务器端反序列化ScanfAppTaskInfo对象
  try
                    {
                        buffer = new byte[data.Length - leng];
                        for (int j = 0; j < data.Length - leng; j++)
                            buffer[j] = data[leng + j];
                        memStream = new MemoryStream(buffer);
                        memStream.Seek(0, SeekOrigin.Begin);
                        obj = formatter.Deserialize(memStream);
                        memStream.Close();
                        if (obj != null && obj is ScanfAppTaskInfo)
                            sataskinfo = (ScanfAppTaskInfo)obj;
                        MessageBox.Show(sataskinfo.ScanfRes);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                        MessageBox.Show(ex.StackTrace);
                        MessageBox.Show(ex.Source);
                    }
实在是看不出来这序列化和反序列化这两个类有什么区别,大家帮忙看看