. NET and Javascript mixed programming series (how to penetrate the boundaries of. NET and JS)

Source: Internet
Author: User

The previous study on how to do CS and JS Mutual transparent access to the idea, after some practice, here to make a small demo.

Here's the first demo class:

    public class Testcsclass    {public        testcsclass ()        {        }        //Overload public        virtual object if overridden Method1 ()        {            Console.WriteLine ("CS call!!");            Return "Hello CS";        }    }

Simple C # code, and then there's an overloaded method Metod1


    [SharpKit.JavaScript.JsType (SHARPKIT.JAVASCRIPT.JSMODE.CLR, Precode = @ "Try{if (typeof (Global). jstypes)! = ' undefined ') {    jstypes = Global. Jstypes;}} catch (e) {} "        )] public    class Testjsclass:testcsclass    {public        string Arg1 {get; set;}        public void Funa (string arg)        {                    } public        Testjsclass ()        {        }    }
This is the class that will generate JS, inherit and Testcsclass, have a unique property, and one more field, and overload a method.

Then there is the proxy JS class, which binds C # objects and has some basic information about inheriting classes

   C # proxy class    namespace proxy    {        //js agent class        [SharpKit.JavaScript.JsType (TargetType = typeof ( Cslibforjs.testcsclass)] public        class Testcsclass        {public            string __basetypename = " Cslibforjs.testcsclass ";            Private object _extobject = null;            Public Testcsclass ()            {               _extobject = Clrhelper.callctor (this);            }            Public virtual Object Method1 ()            {               return clrhelper.callclr (_extobject, "Method1");            }        }            }
Of course this is also C #, or you do not compile the past.

CALLCLR is a reflection call (the performance of what the later again.) )

Let's take a look at the JS code generated by 2 classes:

/* Generated by Sharpkit 5 v5.4.4 */try{if (typeof (Global). jstypes)! = ' undefined ') {jstypes = Global. Jstypes;}} catch (E) {}if (typeof (jstypes) = = "undefined") var jstypes = [];var Cslibforjs$testjsclass = {fullname: "Cslibforj  S.testjsclass ", Basetypename:" Cslibforjs.testcsclass ", AssemblyName:" Cslibforjs ", Kind:" Class ", definition:            {ctor:function () {this._arg1 = null;        CsLibForJS.TestCSClass.ctor.call (this);        }, arg1$$: "System.String", Get_arg1:function () {return this._arg1;        }, Set_arg1:function (value) {this._arg1 = value; }, Funa:function (ARG) {}}};    Jstypes.push (Cslibforjs$testjsclass); var cslibforjs$proxy$testcsclass = {fullname: "CsLibForJS.Proxy.TestCSClass", Basetypename: "System.Object", AssemblyName: "Cslibforjs", Kind: "Class", definition: {ctor:function () {this.__basetypename = "CSLIBFORJS.TESTCSCL";            This._extobject = null;            System.Object.ctor.call (this);        This._extobject = CsLibForJS.ClrHelper.CallCtor (this);        }, Method1:function () {return CSLIBFORJS.CLRHELPER.CALLCLR (This._extobject, "Method1"); }    }}; Jstypes.push (Cslibforjs$proxy$testcsclass);
In order to avoid confusion, the name space of the proxy class is replaced in the jsclr when dealing with the class relationship.

Then we start the first call:

First start with JS:

var csobj = new CsLibForJS.TestCSClass.ctor (), var jsobj =new CsLibForJS.TestJSClass.ctor (); Log ("Jscall"); log (csobj. Method1 ()); log (jsobj. Method1 ());

New out a basic class, new out an inheritance class, a proxy class,

In Jsobj, there is no Method1, execution, output is: (>> start with JS output)

>>jscallcs call!! >>hello CSCS call!! >>hello CS

After binding the underlying object, our JS class can directly have a property pointing to the underlying class of C #, so the method in the object class can be called directly through reflection when invoking the current method.

Then we change the JS class to overloaded Method 1:

    public class Testjsclass:testcsclass    {public        string Arg1 {get; set;}        public void Funa (string arg)        {                    } public        Testjsclass ()        {        } public        override Object Method1 ()        {            return "Hello JS";        }    }

Re-execution:

>>jscallcs call!! >>hello Cs>>hello JS
We have covered the original Method1 method from JS, (nonsense--。。。 )

Let's go on ... The CS method is called from CS, and the overloaded

Create a new JS proxy class:

Namespace Jsproxy    {public        class TestCSClass:CsLibForJS.TestCSClass        {            public Jint.Native.Object.ObjectInstance __bindjsobject;            Public Testcsclass ()            {                            }            //overloaded method public            override Object Method1 ()            {                //overridden                if (__ Bindjsobject.hasproperty ("Method1"))                {                    return __bindjsobject.get ("Method1"). As<jint.native.function.functioninstance> (). Call (__bindjsobject,new jsvalue[0]);                }                else                {                    return base. Method1 ();}}}    

Call Procedure:

            Testcsclass Localclass = new Jsproxy.testcsclass ()            {                __bindjsobject =                    engine. Execute ("New CsLibForJS.TestJSClass.ctor ()")                        . Getcompletionvalue ()                        . As<jint.native.object.objectinstance> ()            };            Console.WriteLine ("Cscall:" + localclass. Method1 ());
Perform:

Cscall:hello JS

It looks like a heavy load, we're just changing our hand js.

Remove this sentence:

   public override Object Method1 ()        {            return "Hello JS";        }

Perform:

Cscall:hello CS

Personalized performance test included:

var sw = new Stopwatch ();            Sw. Start ();            for (int i = 0; i < 10000*10; i++)            {                localclass = new Jsproxy.testcsclass ()                {                    __bindjsobject = ctor. Construct (new jsvalue[0])                };                Localclass. Method1 ();            }            Sw. Stop ();            Console.WriteLine ("Looptime:" + SW. Elapsedmilliseconds);

looptime:6219


Thank you for watching ~









Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

. NET and Javascript mixed programming series (how to penetrate the boundaries of. NET and JS)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.