.NET to Javascript compiler

C# code   Html page   Start
public class InteropApp : HtmlGlobal
{
    delegate string AllParameterTypes(char c, string text, bool b, int i, double dbl, JDate dt, JObject st);

    public static void Start()
    {
        JQueryObject jqo = JQuery.Select("#start");
        jqo.Click(Execute);
    }

    static void Execute(JQueryEvent e)
    {
        HtmlEvent ev = e.OriginalEvent;
        // HtmlInputTextElement result = (HtmlInputTextElement)document.getElementById("result");

        HtmlInputText result = (HtmlInputText)JQuery.Select("#result")[0];

        JArray ja = Eval<JArray>("ar");
        StructJObject sjo = StructJObject.Instance;
        AllParameterTypes allParameterTypes = Eval<AllParameterTypes>("AllParameterTypes");
        Set("string", Get<string>("string") + "_changed");
        Set("bool", !Get<bool>("bool"));
        Set("int", Get<int>("int") + 1);
        Set("double", Get<double>("double") + 1);
        Set("date", Get<JDate>("date").AddDays(1));
        Set("char", (char)(Get<char>("char") + 1));
        sjo.PString += "_changed";
        ja.Set(0, ja.Get<string>(0) + "_changed");

        result.InnerText = string.Format(
            "string = '{0}'\r\n"
            + "bool = '{1}'\r\n"
            + "int = '{2}'\r\n"
            + "long = '{3}'\r\n"
            + "double = '{4}'\r\n"
            + "date = '{5}'\r\n"
            + "char = '{6}'\r\n"
            + "struct.string = '{7}'\r\n"
            + "struct.int = '{8}'\r\n"
            + "struct.double = '{9}'\r\n"
            + "struct.date = '{10}'\r\n"
            + "struct.bool = '{11}'\r\n"
            + "ja<string>(0) = '{12}'\r\n"
            + "ja<int>(1) = '{13}'\r\n"
            + "allParameters() = '{14}'\r\n"
            + "struct.char = '{15}'\r\n"
            + "struct.string = '{16}'\r\n"
            + "struct.int = '{17}'\r\n"
            + "struct.double = '{18}'\r\n"
            + "struct.date = '{19}'\r\n"
            + "struct.bool = '{20}'\r\n",
            window.Eval<string>("string"),
            window.Eval<bool>("bool"),
            window.Eval<int>("int"),
            window.Eval<long>("int"),
            window.Eval<double>("double"),
            window.Eval<JDate>("date"),
            window.Eval<char>("char"),
            sjo.Get<string>("string"),
            sjo.Get<int>("int"),
            sjo.Get<double>("double"),
            sjo.Get<JDate>("date"),
            sjo.Get<bool>("bool"),
            ja.Get<string>(0),
            ja.Get<int>(1),
            allParameterTypes('c', "string", true, 12347, 123.456, new JDate(), sjo),
            sjo.PChar,
            sjo.PString,
            sjo.PInt,
            sjo.PDouble,
            sjo.PDateTime,
            sjo.PBool
            );
    }

    public abstract class StructJObject : JObject
    {
        [JScriptInlineMethod("struct")]
        internal static StructJObject TestInstance(int x1, int x2)
        {
            return HtmlWindow.Invoke<StructJObject>(x1, x2);
        }

        internal static StructJObject Instance
        {
            [JScriptInlineMethod("struct")]
            get
            {
                return HtmlWindow.Invoke<StructJObject>();
            }
        }

        [JScriptSpecialName("char")]
        public abstract char PChar
        {
            get;
            set;
        }

        internal string PString
        {
            [JScriptInlineMethod("{0}.string")]
            get
            {
                return HtmlWindow.Invoke<string>(this);
            }

            [JScriptInlineMethod("{0}.string={1}")]
            set
            {
                HtmlWindow.Invoke(this, value);
            }
        }

        [JScriptSpecialName("double")]
        public abstract double PDouble
        {
            get;
            set;
        }


        [JScriptSpecialName("int")]
        public abstract int PInt
        {
            get;
            set;
        }

        [JScriptSpecialName("date")]
        public abstract JDate PDateTime
        {
            get;
            set;
        }

        [JScriptSpecialName("bool")]
        public abstract bool PBool
        {
            get;
            set;
        }
    }
}

static class JQuery
{
    [JScriptInlineMethod("$({1})")]
    internal static JQueryObject Select(string selector)
    {
        return HtmlWindow.Invoke<JQueryObject>(selector);
    }
}

public abstract class JQueryObject : JObject
{
    [JScriptSpecialName("click")]
    public abstract void Click(Action<JQueryEvent> observer);

    [JScriptSpecialName("")]
    public HtmlElement this[int index]
    {
        [JScriptInlineMethod("{0}[{1}]")]
        get
        {
            return HtmlWindow.Invoke<HtmlElement>(this, index);
        }
    }

    [JScriptSpecialName("length")]
    public abstract int Length
    {
        get;
    }
}

public abstract class JQueryEvent : JObject
{
    [JScriptSpecialName("originalEvent")]
    public abstract HtmlEvent OriginalEvent
    {
        get;
    }
}

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
    <meta http-equiv="x-ua-compatible" content="IE=11;IE=10;IE=9;IE=8;IE=7" />
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <script type="text/javascript" language="javascript" src="js/interop.js"></script>
<script type="text/javascript" language="javascript">
	var char = "c";
	var string = "text";
	var bool = true;
	var int = 12345;
	var double = 123.456;
	var date = new Date();
	var struct = { "char": 'c', "string" : "text", "bool" : true, "int" : 12345, "double" : 123.456, "date" : new Date() };
	var ar = [ "text", 12345, "c", false, 123.456, new Date() ];
	function AllParameterTypes(c, s, b, i, dbl, dt, st)
	{
	return "char:" + c + "\r\n"
	+ "string:" + s + "\r\n"
	+ "bool:" + b + "\r\n"
	+ "int:" + i + "\r\n"
	+ "double:" + dbl + "\r\n"
	+ "struct: {'string':" + st.string + "}\r\n"
	;
	}
	$(document).ready(function()
	{
	var x = $('#start');
	// x.click(function(e) { alert(e.srcElement.tagName); });
	// debugger;
	// alert($('#result')[0].style.width);

	});

</script>
</head>
<body onload="javascript:StartScript();">
	Interop.html: .NET - Javascript communication. A sample for JQuery integration
	<br/>
	<input type="button" id="start" value="Start"/>
	<br/>Result: <textarea id="result" style="width:400px;height:400px;"></textarea>
	
</body>
</html>
© 2009 JScriptSuite. All rights reserved.