Use Eclipse to develop C# apps

author-image
CIOL Bureau
Updated On
New Update

BANGALORE, INDIA: Eclipse is one of the most popular open source IDEs used by developers for building applications on Java and other open source programming languages with ease. A developer would never have chosen Eclipse for building Microsoft .NET Framework apps until Emonic was released. Emonic, short for Eclipse Mono Integration is a plug-in that gives Eclipse the capability to provide a development environment for building C# applications with .NET Framework.

Advertisment

Though Eclipse platform supported development of C and C++ based applications through C Development Toolkit (CDT), but that toolkit couldn't be used for C# development. This is because apps built with C# required .NET Framework to run. Emonic acts as the integration bridge between Eclipse and Mono, which is the open source adaptation of .NET Framework libraries, and thus providing the ability to work with C# and .NET in Eclipse.

Let's see how Eclipse can be transformed to support C# development with the use of Emonic plug-in, whereby one can develop, build and debug C# apps, and migrate existing .NET Framework projects onto Eclipse workspace from Visual Studio.

Emonic with NAnt
Emonic is an Eclipse plug-in that allows you to build C# apps with Mono or the Microsoft .NET Framework. It provides a project wizard, editor, auto-builder and launcher for C# projects. Building a C# project using Emonic alone can be a difficult task. To make this easier, you can use Nant, a .NET build tool which works similar to Ant and supports .NET Framework version 1.0 to version 3.5. The latest NAnt build tool is in version 0.86 Beta 1 and can be downloaded from nant.sourceforge .net. Emonic plug -in for Eclipse can be installed in either of the two ways: downloading from SourceForge or updating Eclipse through an update site.

Advertisment
To set the development environment of Eclipse for .NET, we need to add the corresponding framework through the Preferences option.

The Emonic plug-in can be downloaded as a zipped file from emonic.sourceforge.net. To install this plug-in into Eclipse, just extract the zipped file into a temporary directory, and then copy the contents of features folder into the features folder of your Eclipse installation, and in the same way copy the contents of the plug-ins folder into the plug-ins folder of your Eclipse installation.

 

Advertisment

We can use an update site for installing Emonic plug-in. For this, open Eclipse IDE and go to Help > Software Update > Find and Install. And in the following window, select the 'Search for new features to install' option and click on 'Next'. Click on New Remote site and input http:// emonic.sourceforge.net/up datesite/internap/site.xml as the URL for the update site and click on 'Finish' to initiate the installation.

Emomic provides the outline of the C# file that is opened when the Eclipse environment is switched to the .NET Framework perspective.

Prerequisites
Since we are talking of developing .NET applications in Eclipse, it's understood that an Eclipse IDE will be required. Here, we used Eclipse Europa, while Eclipse 3.1 or a later version is required by Emonic plug-in. Also, as Emonic support .NET Framework 2.0, we will require a .NET Framework V2.0 SDK for that. This SDK is provided in the Microsoft DVD of this issue. From Developer\ Tools folder on the DVD, install the DotNetSDK_V2.exe on your system. Also install the NAnt 0.86 Beta 1 by unzipping the downloaded file contents into your root directory.

Advertisment

Setting up .NET environment for Eclipse
Once the requisites are met, and the installation of Emonic on Eclipse is completed, upon restart of Eclipse, you'll be able to use Emonic for .NET development. You can set the perspective of the Eclipse IDE through Window > Open Perspective > Other and then selecting the .NET option from the list. This will set the Eclipse environment in .NET / Mono development mode, but we still have to set the framework definition for either Mono or .NET.

In our case, as we are using .NET Framework, we will set the framework definition through Window > Preferences and then select the Installed Frameworks option under the .NET node. Click on the 'Add' and select Microsoft .NET as Framework Type, and through Browse button set the path for the Framework home directory. Now click on the Building option under .NET node and check that the Emonic build preferences is set as 'nant' for NAnt command. Click 'OK', and now the .NET Framework environment for Eclipse is set.

When creating a build program, remember to specify the NAnt.exe path in the Location and also specify the current project as Working Directory .
Advertisment

 

Advertisment

Demo C# App
Now we have setup the .NET development environment for Eclipse. Let's test it with a small demo application. Create a new .NET project through File > New > .NET Project. On the New .NET Project window, name the project as 'DemoProject' and select the Target Framework as Microsoft-2.0. When you click on 'Finish', a C# project structure gets created.

Now we will add a C# source class file which will display a message in the console. For this, right-click on the src folder under the DemoProject in the Navigator pane, and select New > C# Class. Give the name and the target name as Demo and click on 'Finish'. Add the following code snippet to the Demo class which upon build will display a message on console:

class Demo{
static void Main() { System.Console.WriteLine("Testing Emonic for Eclipse !!!!");
}}

Advertisment

Building and running the Project
The build.xml file which is created with the project automatically is configured with the target output file, source-code directories and other dependencies. As we are using NAnt, we need to create two external programs, one to build and other to run the demoProject. To create a build program, right-click on build.xml, and select Run As > Open External Tools Dialog. Now in the External Tools window, select Program on left pane and press 'New' icon to launch a new configuration wizard.

Give name of the program as Build_DemoProject and under the Main tab, provide the Location of the NAnt.exe and provide the Working Directory to DemoProject directory by clicking the Browse workspace button. Now click on 'Run' to build the project. Once the project is built successfully, expand the bin node of DemoProject directory and you'll see that a demo.exe file has been created as an output.

Now to execute that, we need to create another external program. Name the new External tools programs as Run_DemoProject, and in the Location option, provide the path of the Demo.exe through Browse Workspace button and keep the target for the Working Directory as DemoProject only. Upon clicking the Run button, the message will appear on the console of the Eclipse IDE.

Migrating .NET project from Visual Studio to Eclipse
As we have set the .NET Framework development environment for Eclipse, we can migrate existing .NET projects from Visual Studio to Eclipse and work on them. Not only we can change the code of the application's project but also build and execute the project as well. Here we demonstrate a migration of an existing C# project from Visual Studio to Eclipse, and then we will build the same in Eclipse and execute it.

For the demonstration, we will use a Temperature Converter application build in C#, which takes the input as Fahrenheit and displays the result in equivalent Celsius.

While importing an existing .NET project, you can either select specific files for import, or you can opt to create the complete folder structure in the corresponding project in Eclipse.

Importing the project
The project which we are importing from Visual studio is named as TempConv. Create a new .NET project in Eclipse and name it as TempConv. Now right-click on project directory and select 'Import' , and in the following list select 'File System' and click on 'Next' . In the following Import window, pass the location of the TempConv project in the Visual Studio's workspace and select the files or folders that you wish to move to the Eclipse environment. After the import is done, open the build.xml file and specify the target as well as the output directories. The following code snippet shows that:







Now we can build and execute the application by creating external program following the same steps as we did for the demo application earlier. When we run the project we can can see the output in a form dialog window appearing which prompts for input in form of Fahrenheits and when OK button is clicked, corresponding Celsius result is displayed. The .NET Framework has successfully been set up for development of C# application on Eclipse platform using Emonic and NAnt.

tech-news