反序列化错误InnerException消息是'ValueType'System.Int32'不能为null [英] Error to deserialize The InnerException message was 'ValueType 'System.Int32' cannot be null
问题描述
当我不断收到错误时,它似乎处于不同的函数
When I kept getting the error, it seems to be at different functions
推荐答案
你好aujong,
Hi aujong,
>>" InnerException消息始终相同:'ValueType'System.Int32'不能为空。'"
>>"The InnerException message was always the same: 'ValueType 'System.Int32' cannot be null.'"
是否抛出异常取决于反序列化的消息。例如,如果您的实体是这样的。
Whether the exception will throw depends on the message which was deserialized. For example, If your entity is like this.
public class MyEntity
{
[Required]
public int PropertyA { get; set; }
}
如您所见,PropertyA的类型是int,它不能为null。但是,如果邮件未提供PropertyA的信息或为此属性提供null,则会发生异常。例如,
As you see, the type of PropertyA is int and it can't be null. But if the message doesn't provide the information of PropertyA or provide null for this property, the exception will occur. For example,
<MyEnity>
<PropertyA></PropertyA>
</MyEntity>
<MyEnity>
</MyEntity>
我建议您在deserilizer之前验证xml或json,或者将PropertyA的类型更改为int?(Nullable< int>)可以接受空值。
I suggest you validate the xml or json before deserilizer it or change the type of PropertyA to int?(Nullable<int>) which could accept null value.
最好的问候
李旺
Best Regards
Li Wang
这篇关于反序列化错误InnerException消息是'ValueType'System.Int32'不能为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!