序列化具有IPEndPoint成员

时间:2015-08-14 09:10:20

标签: c# serialization

我有一个具有IPEndPoint成员的对象,但由于它没有无参数构造函数,因此当我尝试通过反射对其进行序列化时出现错误。

我必须序列化NetAudioDispatcher对象列表....这是代码:

[XmlType(TypeName = "CLanReceiverInfo")]
public class CLanReceiverInfo : ISerializable
{
  public IPEndPoint   RxEndPoint;
  public List<IPEndPoint> TxEndPoints;

  public CLanReceiverInfo()
  {
    RxEndPoint = new IPEndPoint(IPAddress.Loopback, 5000);
    TxEndPoints = new List<IPEndPoint>(1){ new IPEndPoint(IPAddress.Loopback, 6000) };
  }

  public CLanReceiverInfo(SerializationInfo info, StreamingContext context)
  {
    try
    {
      // Reset the property value using the GetValue method.
      RxEndPoint = (IPEndPoint)info.GetValue("RxEndPoint", typeof(IPEndPoint));
      TxEndPoints = (List<IPEndPoint>)info.GetValue("TxEndPoints", typeof(List<IPEndPoint>));
    }
    catch (Exception) 
    {      
      RxEndPoint = new IPEndPoint(IPAddress.Loopback, 5000);
      TxEndPoints = new List<IPEndPoint>(1){ new IPEndPoint(IPAddress.Loopback, 6000) };
    }
  }

  void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
  {
    info.AddValue("RxEndPoint", RxEndPoint);
    info.AddValue("TxEndPoints", TxEndPoints);
  }
}


[XmlType(TypeName = "NetAudioDispatcher")]
public class NetAudioDispatcher : ISerializable
{
  public CLanReceiverInfo     ReceiverInfo { get; private set; }


  void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
  {
    info.AddValue("ReceiverInfo", ReceiverInfo);
  }

  public NetAudioDispatcher()
  {
    ReceiverInfo = new CLanReceiverInfo();
  }

  public NetAudioDispatcher(SerializationInfo info, StreamingContext context)
  {
    try
    {
      ReceiverInfo = (CLanReceiverInfo)info.GetValue("ReceiverInfo", typeof(CLanReceiverInfo));
    }
    catch (Exception)
    {
      ReceiverInfo = new CLanReceiverInfo();
    }
  }

}    


[XmlRoot("NetAudioDispatchers")]
public class NetAudioDispatchers
{
  [XmlElement("NetAudioDispatcher")]
  public List<NetAudioDispatcher> Items { get; set; }

  public NetAudioDispatchers() 
  { 
    Items = new List<NetAudioDispatcher>(); 
  }
}

并且(de)序列化:

var xmlSerializer = new XmlSerializer(typeof(NetAudioDispatchers));
StreamReader stream = new StreamReader(NetworkChatDemo.Properties.Settings.Default.NetAudioDispatchers);
dispatchers = (NetAudioDispatchers)xmlSerializer.Deserialize(stream);

有没有办法超越这个?或者我必须改变我的班级设计?

2 个答案:

答案 0 :(得分:1)

您可能会对此帖子感兴趣:https://stackoverflow.com/a/267904/4928207

如果这也不起作用,你将不得不稍微改变一下。 在这种情况下,我建议将IPEndPoint设为私有/受保护成员,该成员作为公共字符串向序列化程序公开。

答案 1 :(得分:0)

我包装了IPEndPoint:

[Serializable]
[XmlType(TypeName = "IPEndPoint")]
public class IPEndPoint
{
  private System.Net.IPEndPoint _EndPoint;
  private string _Address;
  private int _Port;

  public string Address 
  {
    get { return _Address; }
    set 
    {
      _Address = value;
      _EndPoint = new System.Net.IPEndPoint(IPAddress.Parse(_Address), _Port);
    }
  }

  public int Port 
  {
    get { return _Port; }
    set
    {
      _Port = value;
      _EndPoint = new System.Net.IPEndPoint(IPAddress.Parse(_Address), _Port);
    }
  }


  public IPEndPoint()
  {
    Address = "127.0.0.1";
    Port = 0;

    _EndPoint = new System.Net.IPEndPoint(IPAddress.Loopback, 0);
  }

  public IPEndPoint(IPAddress address, int port)
  {
    Address = address.ToString();
    Port = port;

    _EndPoint = new System.Net.IPEndPoint(address, port);
  }

  public IPEndPoint(string address, int port)
  {
    Address = address;
    Port = port;

    _EndPoint = new System.Net.IPEndPoint(IPAddress.Parse(address), port);
  }


  public System.Net.IPEndPoint Value 
  { 
    get { return _EndPoint; } 
  }

  public override string ToString()
  {
    return _EndPoint.ToString();
  }
}