我正在通用Windows平台(Win10)上编写应用程序。在发布模式下运行时,使用自定义Json Converter时遇到问题,这意味着通过.NET Native进行编译。这是我的转换器代码
public class NestedObjectJsonConverter : JsonConverter
{
private string childNodeName;
public override bool CanConvert(Type objectType)
{
return true;
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
if (reader.TokenType != JsonToken.StartObject)
{
return serializer.Deserialize(reader, objectType);
}
var rootObject = JObject.Load(reader);
var childToken = rootObject[childNodeName];
return ServiceModelJsonConvert.DeserializeObject(childToken, objectType);
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
writer.WriteStartObject();
writer.WritePropertyName(childNodeName);
writer.WriteRawValue(JsonConvert.SerializeObject(value));
writer.WriteEndObject();
}
public NestedObjectJsonConverter(string childNodeName)
{
this.childNodeName = childNodeName;
}
}
在释放模式下触发此转换器时。它会出现"创建错误"异常,但在调试模式下运行良好。
我已经记录了我的异常会议
HResult:-2146233088类型名称:Newtonsoft.Json.Json异常,Newtonsoft.Json,版本=8.0.0.0,文化=中性,PublicKeyToken=30ad4fe6b2a6发送消息:Newtonsoft.Json.Json异常:创建时出错'NestedObjectJsonConverter'。--->System.NullReferenceException:对象引用未设置为实例对象的。在Newtonsoft.Json.Serialization.JsonTypeReflector。<>c.b_18_1(对象param)System.Func.Invoke(Runtime.CompilerServices.CallSiteBinderarg)在SharedLibrary+0x3a908c共享库+0x3b1492共享库+0x4d376a位于Newtonsoft.Json.Serialization.JsonTypeReflector。<>c__DisplayClass18_0.b_0(对象[]parameters)---内部异常堆栈跟踪结束---在Newtonsoft.Json.Serialization.JsonTypeReflector。<>c__DisplayClass18_0.b_0(对象[]参数)System.Func.Invoke(Runtime.CompilerServices.CallSiteBinderarg)Newtonsoft.Json.Serialization.JsonTypeReflector.GetJsonConverter(对象attributeProvider)Newtonsoft.Json.Serialization.DefaultContractResolver.SetPropertySettingsFromAttributes(Json.Sserialization.JsonPropertyproperty,Object attributeProvider,字符串名称,类型declaringType,Json.MemberSerialization MemberSerialization,Boolean&allowNonPublicAccess)Newtonsoft.Json.Serialization.DefaultContractResolver.CreateProperty(Reflection.MemberInfo成员,Json.MemberSerialization MemberSerialization)Newtonsoft.Json.Serialization.DefaultContractResolver.CreateProperties(类型类型,Json.MemberSerialization MemberSerialization)Newtonsoft.Json.Serialization.DefaultContractResolver.CreateObjectContract(类型objectType)Newtonsoft.Json.Serialization.DefaultContractResolver.CreateContract(类型objectType)Newtonsoft.Json.Serialization.DefaultContractResolver.RevolveContract(类型类型)Newtonsoft.Json.Serialization.JsonSerializerInternalReader.GetContractSafe(类型类型)Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(Json.JsonReaderreader,Type objectType,Boolean checkAdditionalContent)Newtonsoft.Json.JsonSerializer.DeserializeInternal(Json.JsonReaderreader,键入objectType)Newtonsoft.Json.JsonSerializer.Deserialize(Json.JsonReader阅读器,键入objectType)Newtonsoft.Json.JsonConvert.DeserializeObject(字符串值,类型类型,Json.JsonSerializer设置)
在.NET Native中发现参数化构造函数似乎是导致此问题的根本原因。但这是.NET Native还是JSON.NET的一个版本?
这是一个.net本机错误。它在当前版本中得到了修复