Can Extension Methods Be Called From The Immediate Window

十年热恋 提交于 2019-12-05 00:01:31

Extention methods are syntax sugar. Actually they are implemented static with the this keyword. You can call any extention method using the static method that provides the extention method. Then you should pass the object that is being extended as the first parameter.

It's because the System.Linq namespace is not imported in the current context you are while debugging.

Add

using System.Linq;

in your code.

Example with Visual Studio 2010:

First times with System.LINQ imported, then without using System.LINQ.

EDIT: If the namespace is imported and IntelliSense is displaying the methods, then it might be a bug of the Immediate window. See this bug entry on connect.

Extension methods are just static methods.

You should be able to use e.g. System.Linq.Enumerable.ToList()

The extension method translates to "Enumerable.ToList" The compiler would normally convert

myList.Tolist();

To:

Enumerable.ToList(myList);

during compile time. I believe you can use extension methods from the quickwatch window if you so wanted to.

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!