Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatic code generation from a C# class to JavaScript equivalent

I'd like to expose a class I've written in C# down to the javascript equivalent.

for example I have a class like:

// C# class to represent an Appriaser
public class Appraiser
{
    public Appraiser(appraiserId, appraiserName)
    {
         AppraiserId = appraiserId;
         AppraiserName = appraiserName;
    }
    public int AppraiserId { get; set; }
    public string AppraiserName { get; set; }
}

and I would like the ability to automatically generate a version of this class in javascript

// javascript class to represent an Appraiser
function Appraiser(appraiserId, appraiserName) {
    var self = this;
    self.appraiserid= appraiserId;
    self.appraisername= appraisername;
}

Is this possible with JSON.NET or another method?

like image 918
Mr. Young Avatar asked Feb 12 '13 20:02

Mr. Young


People also ask

Is there an automatic code generator for Windows?

Automatic Code Generator. As a software developer, maybe you have ever dreamed to have an automatic code generator which can generate source code in good formatting with one click. It can definitely save much time and energy when you are working for a large project.

Is automatic code generation from specification languages the future of telecommunication?

Automatic code generation from specification languages becomes more and more accepted within the telecommunications industry. This paper summarizes some of our four-year experience [1–3] in developing advanced automatic code generation techniques for telecommunications standard specification languages such as SDL [ 6] and ASN.1 [ 7 ].

Can a multiscale model be realized by automatic code generation?

Starting with a conceptual model, it is envisaged that a multiscale model may be realized by automatic code generation, following an approach similar to the one proposed by Yang et al. (2004).

Why automatically generate C from MATLAB?

Automatically generating C from MATLAB speeds design exploration and product development iterations.


2 Answers

You could try sharp2Js. You pass it a type and it converts it to a C# string. Use it with a T4template if you want it to output to a js file. For the class in your example, it produces:

Using it with your example:

var str = Castle.Sharp2Js.JsGenerator.
              GenerateJsModelFromTypeWithDescendants(typeof(Appraiser), true, "example");

Outputs:

example = {};

example.Appraiser = function (cons, overrideObj) {
    if (!overrideObj) { overrideObj = { }; }
    if (!cons) { cons = { }; }
    var i, length;
    this.appraiserId = cons.appraiserId;
    this.appraiserName = cons.appraiserName;


    this.$merge = function (mergeObj) {
        if (!mergeObj) { mergeObj = { }; }
        this.appraiserId = mergeObj.appraiserId;
        this.appraiserName = mergeObj.appraiserName;
    }
}

Note: I'm the maintainer of sharp2Js, it's young so not feature-rich yet (any suggestions are welcome) but it may suit your needs for simple uses.

like image 53
Grant H. Avatar answered Oct 13 '22 03:10

Grant H.


You can try JSIL. It will allow You to transform from .Net IL to JavaScript.

like image 26
rumburak Avatar answered Oct 13 '22 02:10

rumburak