Tip: Create Custom Expressions on the base class

While developing OzCode, we work with multiple class types that represent a parsed C# code file: objects that represent things such as if statements, while loops, assignment expressions, and so forth. As you can imagine, the class hierarchy of these objects is quite complex and large – Underneath the base type there are dozens of derived classes, representing every single type of code construct that exists in C#.

When I initially started debugging, I discovered that the data I get when hovering over these objects wasn’t very useful to me:



This happened because the default ToString implementation of the object blurts out the structure of the parsed code, but what I really wanted to see is just the actual C# code that the object translates to.

And so, having already written a method called ToCSharpCode that takes in an object and returns the C# code it represents, I went on to add a custom expression:



The important thing to take away from the last screenshot is that I intentionally expanded the “base” nodes and chose to add the Custom Expression on the the highest base class in the class hierarchy that the custom expression is applicable to. This will ensure that my Custom Expression will appear not only on BinaryOperatorExpressions, but also onIfElseStatementsAssignmentExpressions, and so forth.

Next, I wrote my Custom Expression:



I used C#’s comment syntax (//) to give my custom expression a shorter name: “C#”.
And finally, I starred my Custom Expression, so it will always right next to the variable name: