可以从立即窗口中调用扩展方法 [英] Can Extension Methods Be Called From The Immediate Window

查看:67
本文介绍了可以从立即窗口中调用扩展方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我问这个问题是因为每当我尝试从Visual Studio 2010的即时"窗口中调用扩展方法时,都会出现以下错误:

I ask the question because whenever I attempt to call an extension method from the Immediate window in Visual Studio 2010 I get the following error:

System.Collections.Generic.IEnumerable'不包含'ToList'的定义,也找不到扩展方法'ToList'接受类型为'System.Collections.Generic.IEnumerable'的第一个参数(您是否缺少使用指令还是程序集引用?)

System.Collections.Generic.IEnumerable' does not contain a definition for 'ToList' and no extension method 'ToList' accepting a first argument of type 'System.Collections.Generic.IEnumerable' could be found (are you missing a using directive or an assembly reference?)

如果即时"窗口不支持扩展方法,那为什么当我键入变量(类型为IEnumerable<QueryFilter>)后跟一个点时,IntelliSense会列出所有扩展方法吗?

If the Immediate window doesn't support extension methods, then why is it that when I type my variable (of type IEnumerable<QueryFilter>) followed by a dot, the IntelliSense lists all the extension methods?

我在命令"窗口中键入的内容没有任何问题,因为如果我将其复制并粘贴到我的代码文件中并运行,它将起作用.

There is nothing wrong with what I am typing in the Command window because if I copy and paste it into my code file and run, it works.

使用Visual Studio 2012对相同的解决方案执行相同的操作可以很好地工作.如果我切换回VS2010,问题仍然存在.

With Visual Studio 2012 doing the same thing for the same solution works fine. If I switch back to VS2010 and the problem persists.

推荐答案

此行为是由代码合同,并且不仅限于立即窗口,还有条件断点窗口.

This behaviour is caused by Code Contracts, and is not limited to just the Immediate window but also the Conditional Breakpoints window too.

2016年3月1日更新: 找到了这个

Update March 01, 2016: Found this MSDN Question asking why type resolution is not working in my watch windows. The behaviour described is exactly the same as I experience when using the Immediate Window. The cause is also attributed to CodeContracts and a bug report has been filed on Microsoft Connect. Whether or not the bug is resolve is not indicated.

这篇关于可以从立即窗口中调用扩展方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆