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.


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


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:


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



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


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


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


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


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)"


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


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();

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


Last edited Sep 25, 2009 at 11:13 PM by fickerra, version 6


No comments yet.