Guide to Converting your C# code to ActionScript

CS2AS is a command-line utility that will work as part of any build process, any IDE, in any environment (including Linux and OSX via Mono.)

This guide is simply a suggested starting point on getting your first CS2AS project up and running. It is anticipated that you will customize this process to suit your needs.

This guide will assume you're using some version of Visual Studio and Flex Builder, but neither are required.

Step 1: (Optional) Create a separate project to convert

Typically, you only want to convert a subset of your C# code to ActionScript. To make this easier, it is often useful to create a separate project that points at the same source files as your current project.

Let's walk through an example project. This project is a Visual Studio 2008 console app that consumes some code. We plan to convert the CodeToConvert class to ActionScript, while leaving the console application functional. This will allow both the ActionScript and C# console applications to consume CodeToConvert, while we only had to write CodeToConvert once.

guide1.png

Create a Class Library project to hold all the code we wish to convert to ActionScript.

guide2.png

Delete the auto-generated Class1.cs file and AssemblyInfo.cs

guide3.png guide4.png

Now, we're going to move the project so it's in the same folder as your existing project. First, remove the project from your solution:

guide5.png

Move the .csproj file from its current location into the same folder as your existing project.

Cut:
guide6.png

Paste:
guide7.png

Now in Visual Studio, add the ActionScript project back by using Add -> Existing Project and selecting the .csproj you just moved.

guide8.png

Select the ActionScript project, and click the "Show all Files" button.

guide9.png

Right-click the code you wish to convert, and select Include In Project.

guide10.png

Ensure the project builds. Since CS2AS reads directly from your source files, it's actually not necessary for your project to build without errors. However, it's usually a good idea to be able to build anyway since it assures you that your code is well-formed and allows post-build events to run.

Step 2: Download and extract CS2AS

Download the latest release of CS2AS and extract it somewhere. For our example, it will be in D:\Stuff\CS2AS\

Step 3: Create your Flex project

For this guide, we will create a new Flex project using Flex Builder 3 in d:\temp\FlexExample

guide11.png

Step 4: Set a post-build event that invokes CS2AS.exe

CS2AS has been tested on over 20,000 lines of C# code and it converts it all within just a few seconds. This makes it quite suitable for a post-build event. We will supply CS2AS.exe with two parameters:

1. Path to ActionScript's .csproj file: We will use the $(ProjectPath) visual studio command to automatically insert this path.
2. Output directory: This tells cs2as where to place the ActionScript files. We will point this at our Flex src folder.

To set your post-build event, right click on the ActionScript project, select Properties. Then select Build Events. Our post-build event will be:

D:\Stuff\CS2AS\CS2AS.exe "/out:d:\temp\FlexExample\src" "/csproj:$(ProjectPath)"


guide12.png

Now build your ActionScript project and all of your code will be converted.

Step 5: Link supplemental ActionScript code

CS2AS provides ActionScript code to implement the base class libraries that exist in .net. This code supplements your converted ActionScript code. This code lives within the System directory of where you extracted CS2AS.

You can copy this System directory to your flex src directory, but I recommend you use a link so your projects auto-update when CS2AS updates.

mklink /D System D:\Stuff\CS2AS\System


guide13.png

Note that mklink is not available on XP and older Windows operating systems. On XP and below, you must copy the directory manually.

Step 6: Done!

Now simply write your Flex code and run it.

import ExampleProject.CodeToConvert;
private function init():void
{
	var c:CodeToConvert = new CodeToConvert();
	trace(c.DoStuff(3));
}

You can examine your src directory to see the converted code.

guide14.png

Last edited Sep 26, 2009 at 12:13 AM by fickerra, version 6

Comments

No comments yet.