当我尝试反序列化可为空的 TimeSpan 属性时出现 Argumentnull 异常。
public class SimpleClass
{
private TimeSpan? m_WorkStartHr;
public TimeSpan? WorkStartHr
{
get { return m_WorkStartHr; }
set { m_WorkStartHr = value; }
}
}
public class Program
{
static void Main(string[] args)
{
JavaScriptSerializer serializer = new JavaScriptSerializer();
TimeSpan? dt = new TimeSpan(288000000000);
SimpleClass instance = new SimpleClass();
instance.WorkStartHr = dt;
string jsonStr = serializer.Serialize(instance);
//This code throws the exception
SimpleClass newInstance = serializer.Deserialize<SimpleClass>(jsonStr);
}
}
注意:如果我们使 WorkStartHr 不可为空,它可以正常工作。