0

I have an object which has a IPEndPoint member but, since it has no parameterless constructor, I get an error when I try to serialize it through reflection.

I have to serialize a list of NetAudioDispatcher objects....this is the code:

[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>(); 
  }
}

And to (de)serialize:

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

Is there a way to overtake this? Or I have to change my class design?

Barzo
  • 1,039
  • 1
  • 11
  • 37

2 Answers2

1

This post might be of interest to you: https://stackoverflow.com/a/267904/4928207

If that doesnt work either, you'll have to change the class slightly. In that case, i would recommend to make the IPEndPoint a private / protected member, which is exposed to the serializer as a public string.

Community
  • 1
  • 1
Mark Jansen
  • 1,491
  • 12
  • 24
0

I wrapped the 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();
  }
}
Barzo
  • 1,039
  • 1
  • 11
  • 37