我在Mvc Core 2中有一些基本代码试图利用T
和ActionResult<T>
之间的隐式转换。
public ActionResult<IEnumerable<FrequentlyAskedQuestion>> Test()
{
IEnumerable<FrequentlyAskedQuestion> x = new FrequentlyAskedQuestion[0];
return x;
}
我收到此错误
2>Controllers\FaqsController.cs(57,20,57,21): error CS0029: Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<Models.FrequentlyAskedQuestion>' to 'Microsoft.AspNetCore.Mvc.ActionResult<System.Collections.Generic.IEnumerable<Models.FrequentlyAskedQuestion>>'
如果我允许它保留为数组,那就可以了。
public ActionResult<IEnumerable<FrequentlyAskedQuestion>> Test()
{
var x = new FrequentlyAskedQuestion[0];
return x;
}
我可以看到AspNetCore的代码有这个静态隐式运算符,这对于它来说很好用
public static implicit operator ActionResult<TValue>(TValue value)
{
return new ActionResult<TValue>(value);
}