序列化一个具有IPEndPoint成员的对象
本文关键字:IPEndPoint 成员 对象 一个 序列化 | 更新日期: 2023-09-27 18:07:58
我有一个对象,它有一个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>();
}
}
和to (de)serialize:
var xmlSerializer = new XmlSerializer(typeof(NetAudioDispatchers));
StreamReader stream = new StreamReader(NetworkChatDemo.Properties.Settings.Default.NetAudioDispatchers);
dispatchers = (NetAudioDispatchers)xmlSerializer.Deserialize(stream);
有办法超过这个吗?或者我必须改变我的类设计?
您可能会对这篇文章感兴趣:https://stackoverflow.com/a/267904/4928207
如果这也不工作,你将不得不稍微改变类。在这种情况下,我建议将IPEndPoint
作为私有/受保护成员,它作为公共字符串暴露给序列化器。
我包装了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();
}
}