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?
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.
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 ].
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).
Automatically generating C from MATLAB speeds design exploration and product development iterations.
You could try sharp2Js. You pass it a type and it converts it to a C# string. Use it with a T4
template 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.
You can try JSIL. It will allow You to transform from .Net IL to JavaScript.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With