9

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?

Mr. Young
  • 2,364
  • 3
  • 25
  • 41

5 Answers5

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.

Grant H.
  • 3,689
  • 2
  • 35
  • 53
3

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

rumburak
  • 1,097
  • 11
  • 19
1

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.

Bobson
  • 13,498
  • 5
  • 55
  • 80
  • I wasn't really looking for the functionality of methods to get copied. I'd be happy with just the properties. I was taking a look at Script# as a candidate just a few minutes ago but haven't tried it yet. Since .NET provides *functionality* like this already with C# to IL, F# to IL, and VB.NET to IL I would had thought the same thing might had be available to C# to javascript. – Mr. Young Feb 12 '13 at 22:23
1

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.

Jason
  • 3,020
  • 2
  • 24
  • 23
  • 1
    Jason, great find! I actually also found that project sometime ago after I posted this question. Still really great find. I really do feel like VS should be able to convert C# POCOs to Typescript POCOs OOB. – Mr. Young Jan 22 '15 at 18:32
  • 1
    I've found Web Essentials to be an ...well, essential... extension to add to Visual Studio. It has all that ability to take C# classes and make typescript definitions from them - plus automagically handle LESS to CSS to minified CSS, as well as JS to minify JS. It's really a spiffy utility with everything it can do. I'd recommend giving it a shot. I do like the Knockout Generator for making fast structured viewmodels. It's so far the best I've found for giving me a shell to work with inside JS. – Jason Jan 22 '15 at 20:50
0

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#

Mr. Young
  • 2,364
  • 3
  • 25
  • 41