Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/279.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# 空数组的默认值数据批注_C#_.net - Fatal编程技术网

C# 空数组的默认值数据批注

C# 空数组的默认值数据批注,c#,.net,C#,.net,我正在创建一个.NET核心Web API,并希望调用提交客户订单的端点。客户id作为路由参数提供。在请求主体中,可以发送一个对象数组。每个对象都包含产品id及其数量。但此字段是可选的,也可以是空订单(稍后可以添加产品) 所以我从这个DTO开始 public class CreateCustomerOrderByIdDto { [FromRoute] public uint Id { get; set; } [FromBody] public OrderPosit

我正在创建一个.NET核心Web API,并希望调用提交客户订单的端点。客户id作为路由参数提供。在请求主体中,可以发送一个对象数组。每个对象都包含产品id及其数量。但此字段是可选的,也可以是空订单(稍后可以添加产品)

所以我从这个DTO开始

public class CreateCustomerOrderByIdDto
{
    [FromRoute]
    public uint Id { get; set; }

    [FromBody]
    public OrderPosition[] OrderPositions { get; set; }
}

public class OrderPosition
{
    [Range(1, uint.MaxValue)]
    public uint ProductId { get; set; }

    [Range(1, uint.MaxValue)]
    public uint Amount { get; set; }
}
此请求DTO应使
OrderPositions
字段成为可选字段,但在添加项目时,该项目需要两个属性。如果缺少,我想为
OrderPositions
设置一个默认值,所以我认为这个数据注释可以做到这一点

[DefaultValue(新订单位置[0])]

不幸的是,我收到了这个错误消息

属性参数必须是常量表达式“typeof()” 属性参数类型的表达式或数组创建表达式

那么,如何将该字段标记为可选字段并设置默认值呢


当不传递订单位置时,数组将被转换为空数组,这样我就可以避免空检查,并处理从未运行过的循环。

也许您可以使用
列表
而不是数组。然后在构造函数中将其初始化为空列表

public class CreateCustomerOrderByIdDto
{
    public CreateCustomerOrderByIdDto()
    {
        this.OrderPositions = new List<OrderPosition>();
    }
    [FromRoute]
    public uint Id { get; set; }

    [FromBody]
    public List<OrderPosition> OrderPositions { get; set; }
}

public class OrderPosition
{
    [Range(1, uint.MaxValue)]
    public uint ProductId { get; set; }

    [Range(1, uint.MaxValue)]
    public uint Amount { get; set; }
}
public类createCustomerOrderByAddTo
{
public createCustomerOrderByAddTo()
{
this.OrderPositions=新列表();
}
[FromRoute]
公共uint Id{get;set;}
[原文]
公共列表顺序位置{get;set;}
}
公共类OrderPosition
{
[范围(1,单位最大值)]
公共uint ProductId{get;set;}
[范围(1,单位最大值)]
公共单位金额{get;set;}
}

与hphp的答案类似,您可以像设置任何c#类一样设置默认值:


如果您不喜欢这些想法,并且正在使用Newtonsoft.Json,您还可以利用序列化事件,并对这些事件执行默认值设置:

数组的问题是它具有固定的长度。您可以使用
array.resize()
调整数组大小,但这也需要固定长度

更好的方法是使用列表而不是数组:

public class CreateCustomerOrderByIdDto
{
    [FromRoute]
    public uint Id { get; set; }

    [FromBody]
    public List<OrderPosition> OrderPositions { get; set; }
}

无法使用
DefaultValueAttribute
执行此操作。但是您可以通过引入
OrderPosition[]\u orderPositions=neworderposition[0]来实现这一点字段,并将
OrderPositions
属性重写为
get{return\u OrderPositions;}set{u OrderPositions=value??new OrderPosition[0];}
感谢您的回复。我尝试了您的解决方案,并对其进行了一点更改,但在传递此正文
{“orderPositions”:[{}]}
时,两种方法都出现了此错误
执行请求时发生了未经处理的异常。System.NotSupportedException:集合的大小是固定的。在System.Array.System.Collections.IList.Add(Object value)
更新的注释代码是否可以使用
List
而不是Array?@Question3r根据所使用的解析器,列表有效而不是数组的原因是,如果集合已经存在,解析器将只向其中添加parses项。对于数组,长度是固定的,因此无法更改。感谢您的回复。问题是,当我得到这个错误时,
集合的大小是固定的。我想如果我不想让数组为空,就不能使用数组
public class CreateCustomerOrderByIdDto
{
    [FromRoute]
    public uint Id { get; set; }

    [FromBody]
    public List<OrderPosition> OrderPositions { get; set; }
}
public List<OrderPosition> OrderPositions { get; set; } = new List<OrderPosition>();