C# DTO 到 TypeScript 生成器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16656406/
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-08-10 01:35:08  来源:igfitidea点击:

DTO to TypeScript generator

c#typescript

提问by jgauffin

I have a C# library (assembly) which contains a set of DTOs which I use to populate my knockout models (TypeScript).

我有一个 C# 库(程序集),其中包含一组 DTO,我用它来填充我的淘汰模型(TypeScript)。

I would like to make sure that the mapping between the JSON data and the ViewModel is correct.

我想确保 JSON 数据和 ViewModel 之间的映射是正确的。

Are there a tool which can generate TypeScript classes from my C# classes? Something like T4 or similar (or just a command line tool which is run as a POST-build event)?

是否有可以从我的 C# 类生成 TypeScript 类的工具?像 T4 或类似的东西(或者只是作为 POST-build 事件运行的命令行工具)?

(Note that the TypeScript files must be placed in another project than the DTO assembly)

(请注意,TypeScript 文件必须放在 DTO 程序集以外的另一个项目中)

采纳答案by basarat

There are lots of projects that do this. Your best bet todaywill be to assess which option is still actively maintained, and meets yourrequirements

有很多项目可以做到这一点。您今天最好的选择是评估哪个选项仍处于积极维护状态,并满足您的要求

Note

笔记

The world is strongly going TypeScript for both front and backend. Hence the diverse state of cross language tooling.

世界正在大力发展前端和后端的 TypeScript。因此,跨语言工具的多样化状态。

回答by Fenton

There is a new library called TypeScriptDwhich will create .d.tsfiles for your .NET classes.

有一个名为TypeScriptD的新库,它将.d.ts为您的 .NET 类创建文件。

This can be run as a command and works for .NET or winmd.

这可以作为命令运行并适用于 .NET 或 winmd。

回答by cpowers

I had a similar need but found the existing projects were too limiting in one way or another (buggy, no generics, no methods, etc.) so I ended up rolling my own (http://cjlpowers.github.io/TypeScripter/). In my case I was dealing with ServiceContract/DataContract types on the server and needed .d.ts files to aid client side development. The solution is extensible should you need to tweak it.

我有类似的需求,但发现现有的项目在某种意义上过于局限(错误、没有泛型、没有方法等),所以我最终推出了自己的项目(http://cjlpowers.github.io/TypeScripter/)。就我而言,我正在处理服务器上的 ServiceContract/DataContract 类型,并且需要 .d.ts 文件来帮助客户端开发。如果您需要调整它,该解决方案是可扩展的。

回答by Rico Suter

Check out the open-source project NSwag: With the GUI, you can select a .NET class from an existing assembly and generate the TypeScript interface for it.

查看开源项目NSwag:使用 GUI,您可以从现有程序集中选择一个 .NET 类并为其生成 TypeScript 接口。

screenshot

截屏

There is also a command line tool and support for T4 templates as well as generation of client code for Web API controllers...

还有一个命令行工具和对 T4 模板的支持以及为 Web API 控制器生成客户端代码......