在开发中定义了一个抽象类,序列号抽象类的子类时,出现如下问题:

Could not create an instance of type TestJsonConvert.IDevice. Type is an interface or abstract class and cannot be instantiated.

解决方法:

在JsonSerializerSettings中设置TypeNameHandling 属性为TypeNameHandling.Auto即可。如下所示:

public abstract class Business
{public string Name { get; set; }
}public class Hotel : Business
{public int Stars { get; set; }
}public class Stockholder
{public string FullName { get; set; }public IList<Business> Businesses { get; set; }
}Stockholder stockholder = new Stockholder
{FullName = "Steve Stockholder",Businesses = new List<Business>{new Hotel{Name = "Hudson Hotel",Stars = 4}}
};string jsonTypeNameAll = JsonConvert.SerializeObject(stockholder, Formatting.Indented, new JsonSerializerSettings
{TypeNameHandling = TypeNameHandling.All
});Console.WriteLine(jsonTypeNameAll);
// {
//   "$type": "Newtonsoft.Json.Samples.Stockholder, Newtonsoft.Json.Tests",
//   "FullName": "Steve Stockholder",
//   "Businesses": {
//     "$type": "System.Collections.Generic.List`1[[Newtonsoft.Json.Samples.Business, Newtonsoft.Json.Tests]], mscorlib",
//     "$values": [
//       {
//         "$type": "Newtonsoft.Json.Samples.Hotel, Newtonsoft.Json.Tests",
//         "Stars": 4,
//         "Name": "Hudson Hotel"
//       }
//     ]
//   }
// }string jsonTypeNameAuto = JsonConvert.SerializeObject(stockholder, Formatting.Indented, new JsonSerializerSettings
{TypeNameHandling = TypeNameHandling.Auto
});Console.WriteLine(jsonTypeNameAuto);
// {
//   "FullName": "Steve Stockholder",
//   "Businesses": [
//     {
//       "$type": "Newtonsoft.Json.Samples.Hotel, Newtonsoft.Json.Tests",
//       "Stars": 4,
//       "Name": "Hudson Hotel"
//     }
//   ]
// }// for security TypeNameHandling is required when deserializing
Stockholder newStockholder = JsonConvert.DeserializeObject<Stockholder>(jsonTypeNameAuto, new JsonSerializerSettings
{TypeNameHandling = TypeNameHandling.Auto
});Console.WriteLine(newStockholder.Businesses[0].GetType().Name);
// Hotel

总结

属性 描述
None 0 序列化类型时不要包含.NET类型名称。
Objects 1 序列化为JSON对象结构时包括.NET类型名称。
Arrays 2 序列化为JSON数组结构时包含.NET类型名称。
All 3 始终在序列化时包含.NET类型名称。
Auto 4 当序列化对象的类型与其声明的类型不同时,请包含.NET类型名称。请注意,默认情况下,这不包括根序列化对象。要在JSON中包含根对象的类型名称,必须使用SerializeObject(Object,Type,JsonSerializerSettings) 或Serialize(JsonWriter,Object,Type)指定根类型对象。

Could not create an instance of type T. Type is an interface or abstract class相关推荐

  1. demo4 debug - create class instance - component works as type

    Created by Wang, Jerry, last modified on Feb 23, 2016 要获取更多Jerry的原创文章,请关注公众号"汪子熙":

  2. 在Filfter中使用注解了spring 的bean,报javax.naming.NamingException: Cannot create resource instance...

    为什么80%的码农都做不了架构师?>>>    严重: Servlet /xcfhinvoices threw load() exception javax.naming.Namin ...

  3. Cannot create an instance of class AndroidViewModel (androidx ViewModelProvider AndroidViewModel)

    使用 androidx viewmodel 2.2.0 后,对 ViewModel 的创建有一些变化 原:ViewModelProviders.of(activity).get(ViewModel.c ...

  4. Cannot create an instance of class xxxxBinding

    问题:Cannot create an instance of class com.fawcar.dlife6.appmarket.databinding.ActivityAppSetupBindin ...

  5. eclipse new server Cannot create a server using the selected type 网上有两种办法,其实原理一样...

    eclipse new server Cannot create a server using the selected type 网上有两种办法,其实原理一样 第一种说法: 还真的找到解决的方法了, ...

  6. Tomcat -- Cannot create a server using the selected type

    2019独角兽企业重金招聘Python工程师标准>>> Cannot create a server using the selected type 这句话出现在窗口上面,应该不是很 ...

  7. 从 A/Looper: Could not create epoll instance. errno=24 错误浅谈解决各种 bug 的思路

    今天代码写着写着就莫名闪退了,手机也没有"程序停止运行"的提示,logcat也没有看到蓝色的调用栈log,这样的闪退最是蛋疼了,还好必现.复现几次之后,终于从logcat中看到了一 ...

  8. 解决“在eclipse中配置Tomcat时,出现Cannot create a server using the selected type的错误”的问题...

    在eclipse中配置Tomcat时,出现"Cannot create a server using the selected type"的错误. 出现原因:Tomcat重新安装, ...

  9. 解决问题spring加载配置文件aop:before 报错: Cannot create inner bean '(inner bean)' of type...

    解决<aop:before> <aop:after>报如下错误的问题: 1.把aspectjweaver的版本换成aspectjweaver-1.6.9.jar,我项目中用1. ...

最新文章

  1. PostgreSQL — 安装
  2. 学习笔记——Java
  3. 深入浅出之string
  4. 资源文件(.RES)的应用
  5. 机器学习实战之KNN实现mnist手写数字分类
  6. vue实现下载pdf文件
  7. 手机PIN码解锁过程
  8. for循环、break和continue、循环的嵌套、white和do-white循环
  9. js截取字符串第一个和最后一个字符
  10. iphone里如何实现像图片浏览那样的自动隐藏和导航条和工具栏
  11. TXTReader功能之一:HotKey
  12. (上)苹果有开源,但又怎样呢?
  13. 创建office vsto外接程序,开发wps插件,配置启动wps进行调试
  14. 嵌入式开发常用到的在线工具
  15. 科普爱好者不可错过的11本科技简史
  16. 古月居ros第十讲遇到的问题
  17. matlab编程与最优化设计应用,MATLAB编程与最优化设计应用
  18. CSDN在线公开课日历【十月】
  19. 百名高管畅谈2019人工智能走向
  20. 移相全桥DCDC峰值电流控制PCMC和电压模式控制VMC对比

热门文章

  1. python自动化面试常问问题_Python自动化测试常见面试题及解析答案
  2. 确认支付9.2亿美元现金:特斯拉偿还可转换债券债务
  3. Esper入门及示例(CEP引擎)
  4. 2048小程序源码(html网页小游戏)
  5. Linux环境下Verilog电路的前后仿真及版图规划
  6. 了解DBHelper类
  7. C++中 cin 的详细用法
  8. LNK2019:无法解析的外部符号, _imp_OpenProcessToken, 该符号在函数main中被引用
  9. python pyc文件使用,使用pyinstaller逆向.pyc文件
  10. iperf测带宽使用方法