Looking for existing, proven, solutions for quickly generating a client-side javascript object model that represents an existing c# object. I imagine there is a T4 template or some other approach out there but I lack the terminology to find it. I'm not talking about serialization to get the JSON representation of an existing c# object instance or anything to do with deserialization. I simply want to generate the javascript object model's for 20+ c# objects and I want to be able to re-generate them at a moments notice if the c# code changes.
Over-simplified example of what I'm looking for:
C# code:
[Serializable()]
public class Cat
{
public string Name { get; set; }
public string Breed { get; set; }
}
Javascript object model to be generated:
function Cat()
{
this.Name = "";
this.Breed = "";
}
@Baszz
JSON is a text-based standard for data interchange and that's not what I'm looking for. I need to generate a client-side API of 20+ objects that I can put in a javascript file and link that script to my various web pages.
The JavaScriptSerializer can spit out a string like below from a c# object:
{ "Name": "Hayden", "Breed": "Rabbit” }
But this is not the same thing as:
function Cat()
{
this.Name = "";
this.Breed = "";
}
- The JSON string is not a named function.
- All elements are quoted and in the JSON format which would require manual parsing of the string to get it into the format I need.
- You cannot new-up an instance of Cat like below because of #1
var myCat = new Cat();
Not a lot of comments so I’m guessing everyone does this by hand or not at all. Looking at creating my own T4 template to parse the c# files and generate my client-side API’s.
After countless searches I could not find anything close to what I’m looking for. Clearly everyone is caught up in the JSON buzz word the past few years and no one is auto-generating client-side object models. I looked at Codesmith and at T4 templates. Neither has any built in mechanisms for parsing code files. Both require you to jump into reflection to get at properties and their types which rests 100% on the developers shoulders. Which begs the question once you jump through that hoop of writing all that reflection code what benefit does Codesmith or T4 templates give you? Absolutely nothing.. You mind as well place your reflection code in a re-usable class library and call it from console application and that’s exactly what I’ve done.
This is an older question, but you could try sharp2Js. It's a library designed to reflect on your classes and generate javascript objects with a couple of utility functions.
Running it against the example you provided (and outputting the string it produces in a T4
template or otherwise):
string modelOutput = Castle.Sharp2Js.JsGenerator.
GenerateJsModelFromTypeWithDescendants(typeof(Cat), true, "example");
Produces:
example = {};
example.Cat = function (cons, overrideObj) {
if (!overrideObj) { overrideObj = { }; }
if (!cons) { cons = { }; }
var i, length;
this.name = cons.name;
this.breed = cons.breed;
this.$merge = function (mergeObj) {
if (!mergeObj) { mergeObj = { }; }
this.name = mergeObj.name;
this.breed = mergeObj.breed;
}
}
The extra metadata in there is some scaffolding to support collections and complex types with the ability to create inherited objects to override behavior, etc.
Note: I am the maintainer of sharp2Js
, and it's new and doesn't do a lot yet, but perhaps it can help for scenarios like yours.
I was thinking about the same idea and i searched a little bit about it i found this link maybe it's useful: http://igorshare.wordpress.com/2012/08/20/roslyn-c-is-cheating-on-me-with-javascript-or-how-to-compile-c-into-javascript/
I am not sure if I could understand what you are talking about, but if you use Reflection you can obtain data about the C# code to generate information for the javascript object.