in
Forums
Blogs
DevExpress.com
Client Center
Support Center
DevExpress Channel

Mark Miller

IDE Team Discussion - Using CodeRush Templates to Generate Code

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:

«:ccsr»

public override void WriteData(BinaryWriter writer)
{
 base.WriteData(writer);
 «ForEach(Field in this, WriteField)»}
 
public override void ReadData(BinaryReader reader)
{
 base.ReadData(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:

TypeImplements(«?Get(itemType)»,System.Boolean)

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.

Published Sep 18 2008, 01:47 PM by Mark Miller (Developer Express)
Filed under:
Technorati tags: CodeRush

Comments

 

Glen Germaine said:

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 http://tv.devexpress.com/ at a better resolution?

Cheers!

G

September 18, 2008 6:44 PM
 

Azret Botash (Developer Express) said:

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... :)

September 18, 2008 8:10 PM
 

CESAR F. QüEB said:

mm....

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

Surely!:

September 18, 2008 11:03 PM
 

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

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

September 19, 2008 9:22 AM
 

Mark Miller (Developer Express) said:

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.

September 19, 2008 1:07 PM
 

wolfgang hauer said:

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

Wolfgang

September 22, 2008 7:49 AM
 

Mark Miller (Developer Express) said:

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.

September 23, 2008 7:19 PM
 

Not Going Dark said:

Coderush

August 29, 2009 1:54 PM

Leave a Comment

(required)  
(optional)
(required)  
Verification code: Required
   
Add

About Mark Miller (Developer Express)

Mark Miller is a C# MVP with strong expertise in decoupled design, plug-in architectures, and great UI. Mark is Chief Architect of the IDE Tools division at Developer Express, and is the visionary force behind productivity tools like CodeRush and Refactor!, as well as the DXCore extensibility layer for Visual Studio. Mark is a popular speaker at conferences around the world and has been writing software for over two decades.
Copyright © 1998-2010 Developer Express Inc.
ALL RIGHTS RESERVED