Some debugging tips on Linq.Expression

Machine Translation Warning:

The content below is mostly translated by machine, and I’ve not got enough time to review the text. So, maybe you’d like to view the Simplified Chinese version of the page.

You can generate IL code, and thereby construct a methods, using ‘System.Linq.Expression’ during runtime.Compared to ‘System.Reflection.Emit.ILGenerator’, use expression trees to construct dynamic methods is an easier way to handle IL code.As you know, IL instructions is just like any kind of assemly, which is rather elaborate. I just cannot handle it well enough 🙁

Of course, you can also use the CodeDOM to write dynamically typed (and dynamic), however, due to the CodeDOM equivalent according to the DOM tusheng code, and then invoke the compiler to compile the code, so there will be some efficiency problems (c.f. Reflection.Emit vs CodeDOM)

Fortunately,. NET Framework 4.0 starts, you can use expression trees to build dynamic methods.For example, a simple input date/time example

Results are as follows

This compilation easier than manually write IL.Can be said to be a major step in a dynamic way.However, the use of dynamically generated delegate also brought some trouble for debugging.For example, as it is the code above, if we enter the date format is incorrect, then an exception is thrown:

Yes, according to this tip, we can determine is ‘lambda_method’ inside an exception has occurred, in this case, because the code is simpler and therefore wrong is very easy to find out.However, when your expression tree is very complex, only know the internal exception occurs in ‘lambda_method’ is not enough.According to the existing debug tools are unable to enter the dynamic method.Therefore, only use indirect means of debugging, such as inserting a test function call:

Or, we can more directly, for example, call ‘Debug.Print’.For the above code, if you receive the error input, you should have the following output:

In fact, if we insert a breakpoint in the ‘TestPoint’ function, run to the breakpoint, you can see the following call stack:

This is another function that reflects the dynamically generated really can’t debug.

But ‘Linq.Expreesion’ has taken into account the problem of debugging at design time, so introducing a secondary attribute.When you are in DEBUG mode, you can find the ‘DebugInfo’ properties in the ‘Expression’ instance.It contains the expression ‘Expression’ contains text.(C.f. debugging expression trees)

Use a text visualizer, you can see the complete contents of the ‘DebugView’ property:

On the specific meaning of each expression in the ‘DebugView’ property, you can refer to the MSDN topics on: debugging expression trees.

Finally, XSerializer I wrote before running a test sample of the generated expression tree … …

Leave a Reply

Your email address will not be published. Required fields are marked *

ERROR: si-captcha.php plugin: GD image support not detected in PHP!

Contact your web host and ask them to enable GD image support for PHP.

ERROR: si-captcha.php plugin: imagepng function not detected in PHP!

Contact your web host and ask them to enable imagepng for PHP.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Content is available under CC BY-SA 3.0 unless otherwise noted.