当我尝试反序列化可以为空的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不可为空,那么它可以正常工作。