Sunday, October 26, 2014

duoco.de - Compile C# code to JavaScript in Visual Studio

DuoCode is an alternative compiler, powered by Microsoft® Roslyn, and integrated in Visual Studio.
It magically cross-compiles your C# 6.0 code into high-quality readable JavaScript code, enabling rapid development of web applications utilizing the extensive features of the C# language, the Visual Studio IDE, and the .NET Framework base class libraries.
Here is a code sample:
// Original C# code
using System;
using DuoCode.Dom;
using DuoCode.Dom.Global;
namespace HelloDuoCode
{
  static class Program
  {
    public class Greeter
    {
      private readonly HTMLElement element;
      private readonly HTMLElement span;
      private int timerToken;
      public Greeter(HTMLElement el)
      {
        element = el;
        span = document.createElement("span");
        element.appendChild(span);
        Tick();
      }
      public void Start()
      {
        timerToken = window.setInterval((Action)Tick, 500);
      }
      public void Stop()
      {
        window.clearTimeout(timerToken);
      }
      private void Tick()
      {
        span.innerHTML = string.Format("The time is: {0}", DateTime.Now); // try to put a breakpoint here
      }
    }
    static void Run()
    {
      System.Console.WriteLine("Hello DuoCode");
      var el = document.getElementById("content");
      var greeter = new Greeter(el);
      greeter.Start();
    }
  }
}
// JavaScript code generated by DuoCode
HelloDuoCode.Program = $declare("HelloDuoCode.Program", System.Object, 0, $HelloDuoCode$Assembly, function($t, $p) {
    $t.Run = function Program_Run() {
        System.Console.WriteLine$10("Hello DuoCode");
        var el = document.getElementById("content");
        var greeter = new HelloDuoCode.Program.Greeter.ctor(el);
        greeter.Start();
    };
});
HelloDuoCode.Program.Greeter = $declare("Greeter", System.Object, 0, HelloDuoCode.Program, function($t, $p) {
    $t.$ator = function() {
        this.element = null;
        this.span = null;
        this.timerToken = 0;
    };
    $t.ctor = function Greeter(el) {
        $t.$baseType.ctor.call(this);
        this.element = el;
        this.span = document.createElement("span");
        this.element.appendChild(this.span);
        this.Tick();
    };
    $t.ctor.prototype = $p;
    $p.Start = function Greeter_Start() {
        this.timerToken = window.setInterval($delegate(this.Tick, System.Action, this), 500);
    };
    $p.Stop = function Greeter_Stop() {
        window.clearTimeout(this.timerToken);
    };
    $p.Tick = function Greeter_Tick() {
        this.span.innerHTML = String.Format("The time is: {0}", $array(System.Object, [System.DateTime().get_Now()])); // try to put a breakpoint here
    };
});

No comments:

Post a Comment