从 C# 类到 JavaScript 等价的自动代码生成

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14841465/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 22:43:05  来源:igfitidea点击:

Automatic code generation from a C# class to JavaScript equivalent

c#javascriptcode-generationjson.net

提问by Mr. Young

I'd like to expose a class I've written in C# down to the javascript equivalent.

我想公开一个我用 C# 编写的类到 javascript 等价物。

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 中自动生成此类的一个版本

// 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?

使用 JSON.NET 或其他方法可以做到这一点吗?

采纳答案by Jason

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

我知道我在回答一个老问题,但现在有几个选择。

There's a Class to Knockout Generatorfor 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.

有一个用于 Visual Studio 的Knockout Generator 类可以为您创建淘汰赛视图模型。此外,如果您进入 Typescript - 在课堂上使用 Web Essentials 的“生成 Typescript”选项将使您获得很多帮助。

回答by rumburak

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

你可以试试JSIL。它将允许您从 .Net IL 转换为 JavaScript。

回答by Grant H.

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:

你可以试试sharp2Js。您向它传递一个类型,它会将其转换为 C# 字符串。T4如果您希望它输出到 js 文件,请将其与模板一起使用。对于您示例中的类,它产生:

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.

注意:我是 的维护者sharp2Js,它很年轻,所以功能还不丰富(欢迎提出任何建议),但它可能适合您的简单使用需求。

回答by Mr. Young

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! :-)

发现 Nik Hilk 有一个名为 Script# 的项目,该项目将从 C# 2.0 代码生成 javascript 等效项。这有效地解决了我将简单的 C# 类转换为 javascript 组件的问题。真正巧妙的是 Script# 也适用于 jQuery 和我的 Knockout 视图模型!:-)

Link to Script#

链接到脚本#

回答by Bobson

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 databack and forth, which is what @jbabey's link is about, but you can't use them interchangeably.

没有什么可以直接从您的 .NET 类创建 javascript 类。您可以来回传递数据,这就是@jbabey 链接的内容,但您不能互换使用它们。

You couldwrite 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 functionalitybetween them.

可以编写 C# 代码,通过使用反射迭代所有公共属性和构造函数,在页面呈现时将类写入页面(以便您可以将 JSON 转换回另一端的对象),但您仍然不会不能在它们之间复制功能