从c#对象自动生成javascript对象模型 [英] automatically generate javascript object model from c# object

查看:165
本文介绍了从c#对象自动生成javascript对象模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

寻找现有的,经过验证的解决方案,以快速生成代表现有c#对象的客户端 javascript对象模型。我想有一个T4模板或其他一些方法,但我缺乏找到它的术语。我不是在谈论序列化以获取现有c#对象实例的JSON表示或与反序列化有关的任何事情。我只是想为20多个c#对象生成javascript对象模型,如果c#代码发生变化,我希望能够立即重新生成它们。

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#代码:

[Serializable()] 
public class Cat 
{ 
    public string Name { get; set; }
    public string Breed { get; set; }
} 

要生成的Javascript对象模型:

Javascript object model to be generated:

function Cat() 
{ 
    this.Name = ""; 
    this.Breed = ""; 
} 






@Baszz


@Baszz

JSON是一种基于文本的数据交换标准,这不是我想要的。我需要生成一个20多个对象的客户端API,我可以将其放在一个javascript文件中,并将该脚本链接到我的各种网页。

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.

JavaScriptSerializer可以从ac#object吐出如下字符串:

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. JSON字符串不是命名函数。

  2. 所有元素都是引用的,并且采用JSON格式,需要手动解析字符串以使其成为我需要的格式。

  3. 由于#1

var,您无法在下面新建一个Cat实例myCat = new Cat();

var myCat = new Cat();

不是很多评论所以我猜大家都是手工做或者根本没做。看看创建我自己的T4模板到解析c#文件并生成我的客户端API。

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.

推荐答案

经过无数次搜索,我无法完成d任何接近我正在寻找的东西。很显然,过去几年里每个人都被JSON的热门话题所吸引,没有人会自动生成客户端对象模型。我看了Codesmith和T4模板。没有任何内置的机制来解析代码文件。两者都要求你跳到反射中以获得属性及其类型,它们在开发人员的肩膀上占有100%的份额。一旦你跳过编写所有反射代码,Codesmith或T4模板给你带来什么好处,这就引出了一个问题?绝对没有..你介意把你的反射代码放在一个可重用的类库中,并从控制台应用程序调用它,这正是我所做的。

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.

这篇关于从c#对象自动生成javascript对象模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆