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

automatically generate javascript object model from c# object - Stack Overflow

programmeradmin3浏览0评论

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 = "";  
}  
  1. The JSON string is not a named function.
  2. 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.
  3. 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.

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 = "";  
}  
  1. The JSON string is not a named function.
  2. 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.
  3. 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.

Share Improve this question edited May 21, 2013 at 13:26 user1228 asked Nov 6, 2011 at 16:57 TugboatCaptainTugboatCaptain 4,3284 gold badges53 silver badges85 bronze badges 2
  • Interesting idea, it might help people trying to providing answers if you explain what you are trying to accomplish by this approach. Eg, what/how these objects are being used. – Darren Reid Commented Nov 9, 2011 at 7:27
  • 1 Hi, I found your question when thinking about writing a similar one and I wonder if we can try to make this more generic and satisfy. Both. This is what I want. I have a REST API done with Entity Framework and MVC4 all with nice automatic tests. Now, I want to create a javascript library that will be used from ly client to call these services. I will enjoy not to have to write the javascript directly but to generated automatically from the API and object model. Of course, I know this can be done with reflection and this is the direction I may go for now. But, finding this done would be better – pabloelustondo Commented Dec 9, 2013 at 17:02
Add a comment  | 

4 Answers 4

Reset to default 6

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.

发布评论

评论列表(0)

  1. 暂无评论