最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Automatic code generation from a C# class to JavaScript equivalent - Stack Overflow

programmeradmin1浏览0评论

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?

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?

Share Improve this question asked Feb 12, 2013 at 20:44 Mr. YoungMr. Young 2,4243 gold badges26 silver badges41 bronze badges 2
  • See stackoverflow.com/questions/3455456/… – Ira Baxter Commented Feb 12, 2013 at 21:48
  • 1 I suppose one might use Swagger (now OpenAPI) for the exact same thing (now) – Mr. Young Commented Nov 17, 2016 at 21:18
Add a comment  | 

5 Answers 5

Reset to default 5

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.

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

I know I'm necro'ing an old question, but there's a few options these days.

There's a Class to Knockout Generator for Visual Studio that will create knockout Viewmodels for you. Also, if you get into Typescript - using Web Essentials "Generate Typescript" option on a class will get you a lot of the way there.

Yes and no. But more "No" than "Yes'.

There's nothing which will directly create javascript classes from your .NET classes. You can pass the data back and forth, which is what @jbabey's link is about, but you can't use them interchangeably.

You could write C# code which would write classes to the page as they render (so that you can convert the JSON back to an object on the other end) by using reflection to iterate over all the public properties and constructors, but you still wouldn't be able to copy functionality between them.

Found out that Nik Hilk has a project called Script# that will generate javascript equivalents from C# 2.0 code. This effectively solved my question for simple C# class to javascript components. The really neat thing is Script# will also work well with jQuery and my Knockout View Models! :-)

Link to Script#

发布评论

评论列表(0)

  1. 暂无评论