CodeRush builds an internal source tree of your C# or VB.NET code. It also has code generating ability - that is, you can take a portion of the tree and generate C# or VB.NET code from it. That got me to thinking about a way of leveraging this ability to build a simple code translator so you could do something like open up a C# file and view the VB.NET equivalent in a tool window (or vice-versa). It turns out the code to do something like this is really straightforward; it's a single method call once you have a reference to the code elements. It doesn't always generate the correct set of code, but it's still really helpful. You basically move the cursor around and the window will display the translated code that is currently "in scope". That is, if you're inside of a method it will show you code for that method. If you move the cursor out to the class, it will show you that class.

To install it, just copy it to your plug-in directory (usually C:\Program Files\Developer Express Inc\DXCore for Visual Studio .NET\2.0\Bin\Plugins\). When you start up Visual Studio a new menu option will appear under DevExpress > Tool Windows > Translator. The window shows a number of different .NET languages, but in reality CR only supports VB.NET and C#.

CR_Translator

BTW - I've mentioned CodeRush/Refactor! and while that's a commercial product you can also download CodeRush by itself for free (and use any third party plug-ins or write your own). You just don't get any of the built-in templates or refactoring tools that the commercial product offers.

Oh, you can download the plug-in from here: http://www.rcs-solutions.com/Download.ashx?File=CR_Translator.zip or from the community plug-in site in a few days.

Links:

http://www.devexpress.com
http://code.google.com/p/dxcorecommunityplugins/


 
Monday, August 18, 2008 2:54:44 PM (Eastern Standard Time, UTC-05:00)
Hi Paul,

Two suggestions:

1. If you would like to syntax-highlight the translated code in your Translator window, drop a DevExpress.CodeRush.UserControls.CodeView control onto that tool window and set its Text property to the code you want to preview, and set its LanguageID property to the language ID you're generating (e.g., "CSharp", "Basic", etc.).

2. It is possible to fix any less-than-ideal code generation issues by manipulating the tree before generation. For example, normally when converting a C# FOR loop to VB, you get a WHILE loop (because VB's dedicated FOR loop is a subset of C#'s FOR loop and the VB WHILE loop is a closer match). However, there may be situations where the implementation of the C# for loop fits within the abilities of the VB FOR loop. You can detect this and then create more accurate code generation by replacing the standard FOR loop with a Visual Basic FOR loop before generating code. If you have specific examples of translation issues and you are interested in ideas on how to improve the quality of the generated code, please let me know (markm at dev express).
Monday, August 18, 2008 5:25:55 PM (Eastern Standard Time, UTC-05:00)
Cool. I figured someone would suggest syntax highlighting/coloring but it looked like a bit more work than I wanted to get into. I'll definitely incorporate it now, though. I'll start to make a note of when the code is a bit off - this wasn't a huge issue, but I noticed in a few cases where I posted both the VB.NET equivalent of some code and someone pointed it out to me.
Tuesday, December 16, 2008 10:55:30 PM (Eastern Standard Time, UTC-05:00)
Paul,

What a fantastic idea! Thank you so much. This is going to be SO useful!!

Yann

PS - now that Mark has seen this, I wonder if he'll include a more sophisticated version of it in CodeRush itself? ;-)
Yann Duran
Name
E-mail
(will show your gravatar icon)
Home page

Comment (Some html is allowed: a@href@title, b, i, strike) where the @ means "attribute." For example, you can use <a href="" title=""> or <blockquote cite="Scott">.  

Enter the code shown (prevents robots):

Live Comment Preview