Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 必需的属性和JSON PropertyName_C#_.net - Fatal编程技术网

C# 必需的属性和JSON PropertyName

C# 必需的属性和JSON PropertyName,c#,.net,C#,.net,我想创建一个具有必需属性和映射JSON名称的类,如下所示: class MyClass { [Required] public string Foo {get; set;} } 这一切都很好。但将其与下面的JSON注释相结合会破坏验证 class MyClass { [Required] [JsonProperty(PropertyName = "bar")] public string Foo {get; set;} } 为什么这里的行为会发生变化?我该如何解决这个问题

我想创建一个具有必需属性和映射JSON名称的类,如下所示:

class MyClass {
  [Required]
  public string Foo {get; set;}
}
这一切都很好。但将其与下面的JSON注释相结合会破坏验证

class MyClass {
  [Required]
  [JsonProperty(PropertyName = "bar")]
  public string Foo {get; set;}
}
为什么这里的行为会发生变化?我该如何解决这个问题?

试试以下方法:

class MyClass
{
    [JsonProperty(PropertyName = "bar", Required = Required.Always)]
    public string Foo { get; set; }
}