. Net and Javascript mixed programming series

Source: Internet
Author: User

The previous article mentions Edge and Nodejs interactions, and it's convenient to provide some extensions for C # through node's module. Here is why to use JS.


1.Sharpkit is a tool for converting the C # language to JavaScript at compile time.

From the trial is still relatively strong, basically support most of the syntax.

Although 2.c# is relatively strong, but in some aspects is still relatively weak, and on some platforms there are some restrictions.

For example, the iOS platform Unity3d not be able to do hot updates, of course, most of the time is not hot update. But it's good to use it if you want to use it.


The problem to be solved?

How to get through the bridge between C # and JS, there are already many implementations, such as the above mentioned edge is a solution.

or jint,jurassic,ironjs such dynamic language, perhaps the performance is not V8 strong, but to meet the daily is basically enough.

Based on the traditional transfer between the values, the module needs to be strictly cut, in the development of the need to reserve the interface, or special module design, will lead to slow progress in development, or lead to some problems in development.

How to achieve seamless access and intrusion is a problem that needs to be considered now.

First of all, we have some existing modules, the old modules may require some of the underlying class library to use inheritance or interface to achieve some design convenience, but cross-language/engine inheritance is basically not achievable.

Then, if in the development of the need to consider JS access, then the ability of developers to bring a test, the need for a Will JS and C # will inevitably bring higher development difficulty, so we do not want to see.

So, what do we expect?

We look forward to switching the code modules at minimal cost after the development is complete, from the point of release to a seamless switching engine, whether Nodejs or the DLR class engine.

Then, why we might need such a switchover. In addition to the above mentioned hot update, then can keep up with the V8 tide has to say is a relatively happy thing, NODEJS,HTML5, and so on, are within the scope of the predictable.

Finally, the question comes, why is C #, not JS?

This has to be from the strong VS, a powerful IDE is the fundamental of efficiency development, and C # has a lower threshold for entry, in the premise of mono, theoretically all platform porting is not a problem, but JS seems to be a little bit lighter, because all the platform has a browser, the old ubiquitous JS engine, and the constant competition of the JS engine is also the cause of JS all over the place.

Idea:

1. First, we want to solve the problem of object inheritance, some people say Oo is dead, this is the benevolent see thing, here is not discussed, we say how to simulate inheritance, the good brothers inherit another, called the combination, we create a base object from the JS layer, and then contains a C # of the objects, Then, by reflection, all the methods are mapped to the JS object by reflection, and we have the first pseudo-object to complete the inheritance. There is no problem with the use, and we need to rewrite some mechanisms only when we have type judgments and type conversions. The process is about this:

C#/js method to invoke an object-"Find JS Method-" Call C # Internal object Method-"return result

C#/js method to invoke an object-"Find JS Method-" return result


2. Interface, because the interface must create an interface to implement the object, then we can not dynamically create the class platform, we have no way, so we have to create a common interface, temporarily called JS Universal interface, (can be generated by the code generator by default) inside is an empty implementation, there is a portal to JS, The process is probably like this:

C #-"interface-" Universal interface-"Invoke JS in the method-" return results

From such a way, we realized the JS implementation of C # interface curve saving way.


3. Overload, similar interface, must be implemented through a proxy mode, the above-mentioned inheritance mode, the JS class must contain a C # of the underlying class, then we will be the underlying class into the so-called proxy class, the proxy class of the method calls to the method in JS, because the proxy class is inherited from the C # class, Then his instantiation should be the C # proxy class (JS Class), then our process is probably like this:

c#-"Method-" proxy class Method-"JS object method (if no overloading)-" Call C # Internal object Method-"return result



As you can see from the above 3 points, we need to provide a proxy class, which is the proxy class for the interface that needs to be exported, or the proxy class that needs to inherit the object. Then through the JS "-" C # Interoperability to achieve the purpose of transparent access.


. Net and Javascript mixed programming series

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.