c# XmlSerializer在反映类型时出现错误

本文关键字:错误 类型 XmlSerializer | 更新日期: 2023-09-27 18:05:21

要传输数据,我使用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; }
        }
    }
}

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

c# XmlSerializer在反映类型时出现错误

我明白了。