我有一个json模式如下:
{
"properties": {
"ArrayTypeProperty": {
"type": "array",
"items": { "$ref": "#/definitions/types" }
}
},
"definitions": {
"types": {
"properties": {
"material": {
"type": "string",
"enum": [
"beds",
"offscreen",
"wind",
"weather"
]
},
"invisible": {
"type": "string",
"enum": [
"air",
"atmosphere"
]
},
"music": {
"type": "string",
"enum": [
"rock",
"metal",
"classic"
]
}
}
}
},
"additionalProperties": false
}
我有以下代码来生成 csharp 类:
var schemaJson = await JsonSchema.FromJsonAsync(m_schema);
var generator = new CSharpGenerator(schemaJson);
var file = generator.GenerateFile("MyClass");
这会生成以下类:
//----------------------
// <auto-generated>
// Generated using the NJsonSchema v10.1.24.0 (Newtonsoft.Json v12.0.0.0) (http://NJsonSchema.org)
// </auto-generated>
//----------------------
namespace MyNamespace
{
#pragma warning disable // Disable all warnings
[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "10.1.24.0 (Newtonsoft.Json v12.0.0.0)")]
public partial class Types
{
[Newtonsoft.Json.JsonProperty("material", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
[Newtonsoft.Json.JsonConverter(typeof(Newtonsoft.Json.Converters.StringEnumConverter))]
public TypesMaterial Material { get; set; }
[Newtonsoft.Json.JsonProperty("invisible", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
[Newtonsoft.Json.JsonConverter(typeof(Newtonsoft.Json.Converters.StringEnumConverter))]
public TypesInvisible Invisible { get; set; }
[Newtonsoft.Json.JsonProperty("music", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
[Newtonsoft.Json.JsonConverter(typeof(Newtonsoft.Json.Converters.StringEnumConverter))]
public TypesMusic Music { get; set; }
private System.Collections.Generic.IDictionary<string, object> _additionalProperties = new System.Collections.Generic.Dictionary<string, object>();
[Newtonsoft.Json.JsonExtensionData]
public System.Collections.Generic.IDictionary<string, object> AdditionalProperties
{
get { return _additionalProperties; }
set { _additionalProperties = value; }
}
}
[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "10.1.24.0 (Newtonsoft.Json v12.0.0.0)")]
public partial class MyClass
{
[Newtonsoft.Json.JsonProperty("ArrayTypeProperty", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
public System.Collections.Generic.ICollection<Types> ArrayTypeProperty { get; set; }
}
[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "10.1.24.0 (Newtonsoft.Json v12.0.0.0)")]
public enum TypesMaterial
{
[System.Runtime.Serialization.EnumMember(Value = @"beds")]
Beds = 0,
[System.Runtime.Serialization.EnumMember(Value = @"offscreen")]
Offscreen = 1,
[System.Runtime.Serialization.EnumMember(Value = @"wind")]
Wind = 2,
[System.Runtime.Serialization.EnumMember(Value = @"weather")]
Weather = 3,
}
[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "10.1.24.0 (Newtonsoft.Json v12.0.0.0)")]
public enum TypesInvisible
{
[System.Runtime.Serialization.EnumMember(Value = @"air")]
Air = 0,
[System.Runtime.Serialization.EnumMember(Value = @"atmosphere")]
Atmosphere = 1,
}
[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "10.1.24.0 (Newtonsoft.Json v12.0.0.0)")]
public enum TypesMusic
{
[System.Runtime.Serialization.EnumMember(Value = @"rock")]
Rock = 0,
[System.Runtime.Serialization.EnumMember(Value = @"metal")]
Metal = 1,
[System.Runtime.Serialization.EnumMember(Value = @"classic")]
Classic = 2,
}
}
现在我想将自定义属性“MyCustomAttribute”添加到“MyClass”的“ArrayTypeProperty”,以便生成的类如下所示:
[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "10.1.24.0 (Newtonsoft.Json v12.0.0.0)")]
public partial class MyClass
{
[Newtonsoft.Json.JsonProperty("ArrayTypeProperty", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
[MyCustom]
public System.Collections.Generic.ICollection<Types> ArrayTypeProperty { get; set; }
}
有人可以帮助我了解我是否可以使用 NJSonSchema 实现这种行为,如果可以,如何实现?