我有这个代码:
public IHttpActionResult Get([FromUri]GetFilesRequest request)
如果请求为null,我想创建一个新的GetFilesRequest,因为在构造函数中我这样做是为了创建类的默认参数:
public GetFilesRequest()
{
Sort = "latest";
Filter = "";
}
所以我在想:
public IHttpActionResult Get([FromUri]GetFilesRequest request = new GetFilesRequest)
但是我收到这个警告:
'request' is of type 'GetFilesRequest'. A default parameter value of a reference type other than string can only be initialized with null.
最佳答案
否 - 默认值必须是编译时常量。您可以改用重载吗?
public IHttpActionResult Get()
{
return Get(new GetFilesRequest());
}
关于c# - 在 C# 中使用可选参数作为对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23770192/