what I am working on – C# code generator (results similar to C++ templates)

After being fed up with some limitations of generics (why isn’t there an IArithmetic interface?), I decided to make a code generator that creates C# code by simply reading templates made in C#.

It’s hard to explain with words, but I have made a video with voice commentary that will explain how it works and show a simple result.

With this code…

var types = new []{"int", "float", "double", "long"};

foreach(var type in types)
{
    $$("public class Vector2_" + type)
    $${         
        $$("public Vector2_" + type + "(" + type + " mX, " + type + " mY)")
        $${
            $$ X = mX;
            $$ Y = mY;
        $$}

        $$("public " + type + " X { get; set; }")
        $$("public " + type + " Y { get; set; }")

        $$("public " + type + " ComponentSum()")
        $${
            $$ return X + Y;
        $$}
    $$}
}        

…my program automatically generates and automatically adds to a .csproj four new classes: Vector2_int, Vector2_float, Vector2_double and Vector2_long.

Watch the video for a better explanation! Thank you :)

Advertisements
1 comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: