C#XmlSerializer在反映类型时出错

时间:2013-03-23 12:39:25

标签: c# serialization reflection xml-serialization

要传输数据,我正在使用XmlSerializer。但是我在下一行遇到了运行时错误:

XmlSerializer serializer = new XmlSerializer(typeof(Packets.Wrapper));

错误是“附加信息:错误反映类型'Packets.Wrapper'。”。 MSDN说我必须使用一个空的构造函数,但它不能解决错误。

该课程如下:

using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;

namespace Packets
{
    [Serializable]
    public class Wrapper
    {
        int _sID = 0;
        int _sSession = 0;
        PacketType _sType = PacketType.None;
        AuthRequest _sAuthRequest = null;
        AuthResponse _sAuthResponse = null;
        ProxyRequest _sProxyRequest = null;
        ProxyResponse _sProxyResponse = null;

        public Wrapper()
        {

        }

        public int ID
        {
            get { return _sID; }
            set { _sID = value; }
        }

        public int Session
        {
            get { return _sSession; }
            set { _sSession = value; }
        }

        public PacketType Type
        {
            get { return _sType; }
            set { _sType = value; }
        }

        public AuthRequest AuthRequest
        {
            get { return _sAuthRequest; }
            set { _sAuthRequest = value; }
        }

        public AuthResponse AuthResponse
        {
            get { return _sAuthResponse; }
            set { _sAuthResponse = value; }
        }

        public ProxyRequest ProxyRequest
        {
            get { return _sProxyRequest; }
            set { _sProxyRequest = value; }
        }

        public ProxyResponse ProxyResponse
        {
            get { return _sProxyResponse; }
            set { _sProxyResponse = value; }
        }
    }

    [Serializable]
    public enum PacketType
    {
        AuthRequest,
        AuthResponse,
        ProxyRequest,
        ProxyResponse,
        None
    }

    [Serializable]
    public enum AuthResult
    {
        Accepted,
        Denied,
        Error
    }

    [Serializable]
    public enum ProxyAction
    {
        Send,
        Response,
        Connect,
        Close
    }

    [Serializable]
    public enum ProxyResult
    {
        Connected,
        Sent,
        Revieved,
        Error
    }

    [Serializable]
    public class AuthRequest
    {
        string username = null;
        string password = null;

        public AuthRequest()
        {

        }

        public string Username
        {
            get { return username; }
            set { username = value; }
        }

        public string Password
        {
            get { return password; }
            set { password = value; }
        }
    }

    [Serializable]
    public class AuthResponse
    {
        AuthResult authResult = AuthResult.Denied;

        public AuthResponse()
        {

        }

        public AuthResult AuthResult
        {
            get { return authResult; }
            set { authResult = value; }
        }
    }

    [Serializable]
    public class ProxyRequest
    {
        ProxyAction _sAction;
        string _sIP = null;
        int _sPort = 0;
        TcpClient _sClient = null;

        public ProxyRequest()
        {

        }

        public ProxyAction Action
        {
            get { return _sAction; }
            set { _sAction = value; }
        }

        public string IP
        {
            get { return _sIP; }
            set { _sIP = value; }
        }

        public int Port
        {
            get { return _sPort; }
            set { _sPort = value; }
        }

        public TcpClient Client
        {
            get { return _sClient; }
            set { _sClient = value; }
        }
    }

    [Serializable]
    public class ProxyResponse
    {
        public ProxyResult _sResult = ProxyResult.Error;
        public StringBuilder _sError = new StringBuilder();
        public StringBuilder _sRecieved = new StringBuilder();
        public TcpClient _sClient = null;

        public ProxyResponse()
        {

        }

        public ProxyResult Result
        {
            get { return _sResult; }
            set { _sResult = value; }
        }

        public StringBuilder Error
        {
            get { return _sError; }
            set { _sError = value; }
        }

        public StringBuilder Recieved
        {
            get { return _sRecieved; }
            set { _sRecieved = value; }
        }

        public TcpClient Client
        {
            get { return _sClient; }
            set { _sClient = value; }
        }
    }
}

我希望你能帮助我并感谢你的时间。

1 个答案:

答案 0 :(得分:0)

我明白了。 TcpClient不可序列化。