且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在JsonConverterAttribute中将属性传递给StringEnumConverter

更新时间:2022-06-02 00:49:41

JsonConverterAttribute 有两个构造函数,其中一个带有参数列表(Object[]).这会从第一个参数映射到该类型的构造函数.

JsonConverterAttribute has two constructors, one of which takes a parameter list (Object[]). This maps to the constructor of the type from the first parameter.

StringEnumConverter 可以使用大多数非默认值来处理此问题构造函数.

StringEnumConverter can handle this with most of its non-default constructors.

第一个在JSON.net中已作废12+

The first one is obsolete in JSON.net 12+

第二个允许您指定 NamingStrategy 类型; CamelCaseNamingStrategy 可以解决问题.实际上,对于所提供的六个构造函数中的三个,这是正确的.

The second one allows you to specify a NamingStrategy Type; the CamelCaseNamingStrategy does the trick. Actually, this is true for three out of the six constructors provided.

注意:另一个构造函数打破了常规,要求NamingStrategy的实例而不是类型.

Note: one other constructor breaks the mold, asking for an instance of a NamingStrategy instead of a type.

它看起来像这样:

[JsonConverter(typeof(StringEnumConverter), typeof(CamelCaseNamingStrategy))]
public MarkerType MarkerType { get; set; }