[英]Why does Visual Studio think my “yield return” method returns a dynamic object?
I've written a method that returns an IEnumerable<T>
using yield return
like so: 我已经编写了一种使用
yield return
返回IEnumerable<T>
的方法,如下所示:
public IEnumerable<Row> ConvertExcelToRows(IExcelDataReader reader)
{
while (reader.Read())
{
var row = new Row();
// do some work. No dynamic objects here whatsoever
yield return row;
}
}
When I consume my method and follow it with LINQ extension methods, Visual Studio shows the return value to be of type dynamic
: 当我使用我的方法并使用LINQ扩展方法时,Visual Studio显示返回值的类型为
dynamic
:
Here is the relevant code leading up to the symptom: 这是导致症状的相关代码:
dynamic data = JsonConvert.DeserializeObject(jsonContent);
using (var stream = await DownloadFile(data.docUrl.ToString()))
using (var excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream))
{
var rows = ConvertExcelToRows(excelReader).ToList<Row>();
}
The type of rows
in the screen shot above is dynamic
when it should be List<Row>
. 上面的屏幕截图中的
rows
类型应该是List<Row>
时是dynamic
。
Why is this happening and how can I fix it? 为什么会发生这种情况,我该如何解决?
(Please note that Row
is just a class I wrote up. It's all primitive properties, nothing dynamic) (请注意,
Row
只是我写的一个类。它都是原始属性,没有动态的)
KEY UPDATE: In the process of improving the screenshot above, I've changed the declaration of excelReader
from var
to IExcelDataReader
. 关键更新:在改进上面的屏幕快照的过程中,我将
excelReader
的声明从var
更改为IExcelDataReader
。 This solved my problem . 这解决了我的问题 。
When I changed back to var
, I saw that the inferred type of excelReader
is indeed the contagious dynamic
. 当我改回
var
,我看到excelReader
的推断类型确实是具有感染力的dynamic
。
Would still love an explanation of why a dynamic input argument to my method would "infect" the type of the output. 仍然会喜欢为什么我的方法的动态输入参数会“感染”输出类型的解释。
dynamic
operates by a contagion principle . dynamic
是根据传染原理进行的 。 If anything coming into an expression is dynamic
, then the compiler can make no guarantees at compile time about what might come out -- so what comes out is considered dynamic
as well. 如果进入表达式的任何内容都是
dynamic
,则编译器无法在编译时保证可能出现的结果-因此,得出的结果也被视为dynamic
。 There's a limit to the static analysis a compiler can do when types may change arbitrarily at runtime. 当类型可以在运行时任意更改时,编译器可以执行的静态分析是有限的。
Therefore, if what's coming out of your expression is dynamic
and you didn't assign the result to an explicitly dynamic
variable, then that's because something dynamic
must have come in. If you didn't give this method call anything explicitly dynamic
, then something you gave it must have been "infected" elsewhere. 因此,如果有什么出来你表达的是
dynamic
,你没有把结果赋值给一个明确的dynamic
变量,则是因为一些dynamic
一定是。如果你没有给这个方法调用任何明确的dynamic
,那么什么您给的消息一定是在其他地方“感染”了。
Something upstream, a parameter to a method, the object you called the method on, or a term in an expression, is dynamic
. 在上游,方法的参数,在其上调用该方法的对象或表达式中的术语是
dynamic
。 You need to find out what. 您需要找出什么。
An obvious candidate is excelReader
: Where did it come from, and where did that object come from? excelReader
是一个很明显的候选人:它是从哪里来的, 那个对象是从哪里来的? What were the parameters (if any) you gave the method, and where did you get them? 您为该方法提供了哪些参数(如果有),您从哪里得到的?
Patient Zero is out there somewhere. 零号病人在那里。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.