Json.Net Serialize an array and deserialize as a list
Here is my class
public class Agency
{
private IList<AgencyCommission> _commission;
public Agency()
{
_commissio开发者_如何学Cn = new List<AgencyCommission>();
}
public string Name {get; set;}
public AgencyCommission[] Commissions
{
get {return _commissions.ToArray();}
private set {_commissions = value;}
}
public void ComissionAdd(AgencyCommission commission)
{
if (commission != null)
{
_commissions.Add(commission);
}
}
}
Serializing it and then deserializing it works perfectly, until I try to add a new commission, it blows an error saying:
System.NotSupportedException : Collection was of a fixed size.
at System.SZArrayHelper.Add(T value)
at Agency.ComissionAdd(AgencyCommission commission) in Agency.cs: line 62
How to make this works ? Custom converter ? Custom Resolver ?
Any sample code highly appreciated !
I found a solution to this:
public AgencyCommission[] Commissions
{
get {return _commissions.ToArray();}
private set {_commissions = new List<AgencyCommission>(value);}
}
精彩评论