您可以编写一个可序列化为字符串的列表类。
未测试示例:
public class AddressList : IList<IPAdress>
{
public string Serialized { get; private set; }
private List<IPAddress> _innerList = new List<IPAddress>();
//To be informed when the list is modified.
public event EventHandler OnModified;
public AddressList(string serializedString)
{
//Init the collection from the serialized string
}
public void Add(IPAdress item)
{
_innerList.Add(item)
//Update the serialized string
UpdateString();
}
public void Remove(IPAdress item)
{
_innerList.Remove(item);
//Update the serialized string
UpdateString();
}
private void UpdateString()
{
//Do your update job.
//Call the event.
if (OnModified != null)
OnModified(this, EventArgs.Empty);
}
//Rest of IList implementation....
}
然后在您的实体中:
public partial class MyEntity
{
private AddressList _addresses;
public IList<IPAdress> Adresses
{
get
{
if (_addresses == null)
{
_addresses = new AddressList(HostList);
//When the list is modified, you want to update your property.
_addresses.OnModified += delegate(object sender, EventArgs e)
{
HostList = ((AddressList) sender).Serialized;
};
}
return _adresses;
}
}
}
这样,当您从列表中添加/删除项目时,字符串会“自动”更新。