IDE Team Discussion - Using CodeRush Templates to Generate Code

18 September 2008

CodeRush users interested in creating templates that generate custom code based on elements inside a container (e.g., fields in a class, methods in a type, types in a namespace, comments in a file, etc.), might want to check out this YouTube video.

In it, the IDE Team discusses some work we're doing, where we need to add custom code to serialize and deserialize the fields of around 30 classes. The solution came in creating a template that iterates through the fields in each class and generates the appropriate serialization or deserialization code for each field.

The main template looks like this:


public override void WriteData(BinaryWriter writer)
 «ForEach(Field in this, WriteField)»}
public override void ReadData(BinaryReader reader)
 «ForEach(Field in this, ReadField)»}

Both the ReadField and WriteField templates will be called once for each field in the active class. Both of these templates have a number of alternate expansions. The expansion ultimately selected for a particular field is determined by context. You can set context with the Context Picker on the lower right of the Template options page.

To make this work, we created a new context, called TypeImplements, because many of the scenarios we needed to respond to were dependent upon the type of the field. For example, one of the alternate expansions for ReadField has this context:


You can pass parameters to contexts (like we've done here), by right-clicking the context in the Context Picker, and selecting "Parameters...".

The expansion for the ReadField template associated with the context above looks like this:

«?Get(itemName)» = reader.ReadBoolean();

«?Get(itemName)» returns the name of the field we're iterating over, while «?Get(itemType)» returns the full type name. Get is a custom StringProvider that you can use to retrieve the value of a template variable stored with the Set StringProvider (the ForEach TextCommand stores the itemName and itemType variables for you automatically before calling the ReadField and WriteField templates).

The new TypeImplements context added to solve this code generation challenge will ship with the next version of CodeRush.

8 comment(s)
Glen Germaine

Hi Mark,  Really interesting stuff, but I gave up 60 seconds into the video - I defy anyone to read the code on the screen on the YouTube video.  

Is there a reason why you chose to put it on YouTube and not on at a better resolution?



18 September, 2008
Azret Botash (DevExpress)

Hi Glen,

This was not meant to be a DevExpress Channel video :)

Jeff just walked in with one of his cameras and recorded some bits... :)

18 September, 2008


A way to shows that new interesting features are coming... because sincerely i was thinking go with the optometrist. :)


18 September, 2008
Dew Drop - September 19, 2008 | Alvin Ashcraft's Morning Dew

Pingback from  Dew Drop - September 19, 2008 | Alvin Ashcraft's Morning Dew

19 September, 2008
Mark Miller (DevExpress)

Thanks for the feedback. I definitely agree with you that the resolution at YouTube sucks. We shot the screen part at 1024x768 with Camtasia, so it should have been better, but even clicking the View in High Quality button at YouTube produces results that requires some imagination to read. I'll talk to our video guy and see if we can get a better quality version up somewhere.

19 September, 2008
wolfgang hauer

Can i access an attribute associated with a class, property? I think about use that new feature with XPO?


22 September, 2008
Mark Miller (DevExpress)

Hi Wolfgang,

You might want to post more specifics of your question to the CodeRush for Visual Studio newsgroup/forum, or you can direct it to support. In general however, the answer is Yes, you can access attributes associated with classes and members, however you might have to write a small amount of code to make this work. CodeRush customers get full source to the ForEach TextCommand, so if you want to add the ability to iterate through all attributes associated with the active member/type, this is easy to do.

23 September, 2008
Not Going Dark


29 August, 2009

Please login or register to post comments.