简体   繁体   English

为什么Visual Studio认为我的“收益回报”方法会返回动态对象?

[英]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.

相关问题 为什么我的非动态类方法返回动态 - Why does my non-dynamic class method return dynamic 当我传递HashSet时,为什么我的泛型类型方法认为T是system.object - Why does my generic type method think T is system.object when I'm passing in a HashSet 为什么IntelliSense认为我的字典中的值是动态的? - Why does IntelliSense think a value from my dictionary is a dynamic? LINQ to SQL为什么在尝试将新对象添加到数据库时认为我的新对象为空? - Why does LINQ to SQL think think that my new object is null when I try to add it to the database? 为什么 yield return 会返回意想不到的结果? - Why yield return returns unexpected result? IEnumerable如何在具有Yield返回的方法中返回元素/项 - How does IEnumerable returns a element/Item in the method which has Yield return 为什么Visual Studio代码样式设置认为DateTime不是内置类型但Guid就像它一样? - Why does Visual Studio code style settings think DateTime is not a built-in type but Guid acts like it is? 动态参数导致编译器认为方法返回是动态的 - Dynamic parameter causes compiler to think method return is dynamic 为什么我的 UWP DispatcherTimer tick 方法仅在附加 Visual Studio 调试器时触发? - Why does my UWP DispatcherTimer tick method only fire when the Visual Studio debugger is attached? 收益率和动态 - Yield return and dynamic
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM