Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to continue debugging after editing a method containing a lambda expression? [duplicate]

Usually, when a method contains a lambda expression somewhere in it, if you edit that method, Visual Studio will say:

Modifying a 'method' which contains a lambda expression will prevent the debug session from continuing while Edit and Continue is enabled.

Is there a way to avoid this error?

In my case, I have a class whose constructor accepts an Action.

Button(Texture2D t2d, Vector2 v2, Action onPress)
...
Button b = new Button(t2d, new Vector2(40, 60), () => { MainStatic.t = t; });

Additional information on this issue:

  • http://blogs.msdn.com/b/visualstudioalm/archive/2014/11/12/support-for-debugging-lambda-expressions-with-visual-studio-2015.aspx
  • http://visualstudio.uservoice.com/forums/121579-visual-studio/suggestions/4079440-edit-continue-allow-the-modification-of-lambdas
like image 445
user1306322 Avatar asked Jan 06 '13 15:01

user1306322


1 Answers

UPDATE: The desired feature was added in Visual Studio 2015, after many requests from users for this feature. This answer, and the question, are now out of date.


Is there a way to avoid this error?

Yes. Remove the lambda from the method. Or, don't edit the method.

Is there a way to avoid this error without removing the lambda from the method and still editing the method?

No. The error message is not lying to you.

The reason for this, if you're curious, is because lambdas are compiled as methods of a nested class, and local variables that the lambda closes over become fields of that class. The edit-and-continue feature rewrites the current method on-the-fly as you edit it, but even simple edits can result in complex changes to those nested classes. Rather than spend an enormous amount of effort on making E&C work for this scenario, and thereby steal resources from other, more valuable features, the debugger team simply made it illegal.

like image 68
Eric Lippert Avatar answered Sep 20 '22 18:09

Eric Lippert