Reusing C# in Your Prism Projects

One of the (few) concerns we hear from people looking to get started with Delphi Prism is that most of the code samples and snippets that are available on the net are written in C#.

Because .NET is fully language agnostic, reusing existing libraries and assemblies written in C# (or any other language) generally is no problem, but for smaller pieces of code – classes or even just small code snippets – it is inconvenient for the Prism developer to start a separate C# project – making manual translation (and sometimes, deeper understanding of the C# language and its intricacies) necessary.

We wanted to make this easier, so for the upcoming February* release of Delphi Prism, we’ve added two new features to the IDE, based on out open source C# to Oxygene tool.

The first is the new Paste C# as Oxygene command in the source editor. If you find snippets of C# code on the web (or elsewhere) that you want to reuse in your project, simply copy them to the clipboard, and then right-click into your code and choose Paste C# as Oxygene from the context menu:

[![](http://blogs.remobjects.com/wp-content/uploads/2010/01/PasteCS.png "PasteCS")](http://blogs.remobjects.com/wp-content/uploads/2010/01/PasteCS.png)
The IDE will translate the code for you, and paste the Oxygene version in. This will work on just about any type of code – from a single line of code or a few compound statements, to entire method bodies, or even full classes. (Because classes are self-contained entities in C#, but split in declaration and implementation in Oxygene, pasting a whole class will actually generate a whole source module, including with “interface” and “implementation” sections. As such, pasting full classes works best when done into an empty file).

All of this is great when you have code snippets, especially from websites, forums or newsgroups. But what about C# files already on disk? That’s where the second feature comes in: if you right-click your Delphi Prism project and go to the Add menu, you’ll see a new Import C# sub-item. Select this, and you’ll be presented with a regular File Open dialog, where you can browse to and select one or more .cs files on your harddisk. When you click Ok, each .cs file will be translated to Oxygene, and a corresponding new .pas file will be added to your project:

[![](http://blogs.remobjects.com/wp-content/uploads/2010/01/ImportCS.png "ImportCS")](http://blogs.remobjects.com/wp-content/uploads/2010/01/ImportCS.png)
How well does this work? C# is a complex language, so while Oxygene provides a complete superset of what can be done in C#, there might be some cases where complicated C# code will confuse the translator, but in the vast majority f cases, the translate code will work as is.

One of our test bases is the entire RemObjects SDK for .NET library, and the tool translates that very satisfactory.

Paste C# and Import C# are probably not features you want to use to blindly translate whole projects with and reuse the resuiting code without further testing or review, but in our experience it is working very well or the intended purpose – reuse onf code snippets found online.

Just like Oxfuscator, covered in my previous post, this is in the current beta drop, so if you already a Delphi Prism user (or buy it now), make sure to apply for the beta.

* the “February” release will ship a bit later than February, this year, as we want to align it with the public release of Visual Studio 2010, which has been announced for April. Internally, we still refer to it as the “February Release”.