C# JsonConstructor不被尊重
我试图通过Json.NET反序列化一个类:C# JsonConstructor不被尊重,c#,json.net,C#,Json.net,我试图通过Json.NET反序列化一个类: public class JobStatus { [JsonConstructor] protected JobStatus() { } protected internal JobStatus( AzureBlobFile blobFile ) { if( blobFile == null ) throw new ArgumentNullException(na
public class JobStatus
{
[JsonConstructor]
protected JobStatus()
{
}
protected internal JobStatus( AzureBlobFile blobFile )
{
if( blobFile == null )
throw new ArgumentNullException(nameof( blobFile ));
}
}
反序列化按如下方式触发:
JsonSerializerSettings jsonSettings = new JsonSerializerSettings()
{
TypeNameHandling = TypeNameHandling.All,
ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor
};
List<AzureBlobFile> blobFiles = blobFolder.GetBlobFiles();
if( blobFiles == null ) return null;
string text = blobFiles.First().DownloadText();
JobStatus junk = JsonConvert.DeserializeObject<JobStatus>( text, jsonSettings );
对反序列化对象的调用总是失败,因为它调用参数化构造函数,而不是用JsonConstructor属性修饰的非参数化构造函数
我是否使用了错误的属性?我的错误:问题是,由于TypeNameHandling有效,而JobStatus是一个父类,因此每个子类JobStatus->BatchJobStatus->AgencyJobStatus[未显示]也必须有无参数构造函数供Json.NET使用。在我这方面工作正常。无法复制,请参阅。