简体   繁体   English

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

[英]Automatic code generation from a C# class to JavaScript equivalent

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或其他方法有关吗?

You can try JSIL . 你可以试试JSIL It will allow You to transform from .Net IL to JavaScript. 它将允许您从.Net IL转换为JavaScript。

You could try sharp2Js . 你可以试试sharp2Js You pass it a type and it converts it to a C# string. 您传递一个类型并将其转换为C#字符串。 Use it with a T4 template if you want it to output to a js file. 如果要将其输出到js文件,请将其与T4模板一起使用。 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. 注意:我是sharp2Js ,它很年轻,但功能不丰富(欢迎任何建议),但它可能适合您的简单用途需求。

Yes and no. 是的,不是。 But more "No" than "Yes'. 但更“不”而不是“是”。

There's nothing which will directly create javascript classes from your .NET classes. 没有什么能直接从你的.NET类创建javascript类。 You can pass the data back and forth, which is what @jbabey's link is about, but you can't use them interchangeably. 你可以来回传递数据 ,这是@jbabey的链接,但你不能互换使用它们。

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. 可以编写C#代码,这些代码会在呈现时向页面编写类(以便您可以通过使用反射来迭代所有公共属性和构造函数,从而将JSON转换回另一端的对象),但您仍然不会能够在它们之间复制功能

Found out that Nik Hilk has a project called Script# that will generate javascript equivalents from C# 2.0 code. 发现Nik Hilk有一个名为Script#的项目,它将从C#2.0代码生成javascript等价物。 This effectively solved my question for simple C# class to javascript components. 这有效地解决了我对简单的C#类到javascript组件的问题。 The really neat thing is Script# will also work well with jQuery and my Knockout View Models! 非常简洁的东西是Script#也适用于jQuery和我的Knockout View Models! :-) :-)

Link to Script# 链接到脚本#

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. 有一个针对Visual Studio的Knockout生成器类 ,它将为您创建淘汰视图模型。 Also, if you get into Typescript - using Web Essentials "Generate Typescript" option on a class will get you a lot of the way there. 此外,如果你进入Typescript - 使用Web Essentials“Generate Typescript”选项可以让你获得很多方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM