T隐式转换为ActionResult <t>不适用于某些类型

时间:2018-06-27 04:44:11

标签: c# asp.net-core type-conversion implicit-conversion

在asp.net核心2.1中,引入了#include <iostream> #include <cstdlib> #include <ctime> using namespace std; int main() { int num, guess; srand((int)time(0)); //generating random number 1-100 num = rand()%100+1; cout<<num<<endl; cout<<"Let's play a game! I'll think of a number and you can try to guess it."<<endl; cout<<"Let's begin! I'm thinking of a number 1-100. What's my number?"<<endl; cin>>guess; while(guess != num) { if(guess<num) { if (guess <(num/2)) { cout<<"-----"<< endl; } else{ cout<<"---"<< endl; } } if(guess>num) { if (guess>(num+num/2)) { cout<<"+++++"<< endl; } else{ cout<<"+++"<< endl; } } cout<<"Guess again" << endl; cin >> guess; } cout<<"########YOU WON!########"; } ,并定义了从AsctionResult<T>T的隐式类型转换

在我的代码中,某些转换出现错误。例如:

ActionResult<T>

尽管相同的转换对于其他类型也可以正常工作。对于我的代码中定义的几种类型,我也会遇到此错误。

上述错误的代码为:

Error   CS0029  Cannot implicitly convert type 'System.Collections.IEnumerable' to 'Microsoft.AspNetCore.Mvc.ActionResult<System.Collections.IEnumerable>'

其中 public async Task<ActionResult<IEnumerable>> GetByFilter([FromBody]EntityListRequest request) { return await _entityHandler.GetByFilter(request); } 是:

_entityHandler.GetByFilter()

1 个答案:

答案 0 :(得分:0)

好的。显然,这对于某些接口会失败。此质量检查中提供了更多信息:Why does this implicit type conversion in C# fail?

一种解决方法是使用ActionResult的构造函数:

            return new ActionResult<IEnumerable>(myData);