简体   繁体   English

XML序列化Roslyn SyntaxTree?

[英]XML serialize Roslyn SyntaxTree?

I'm looking for options to convert C# to XML and also deserialize back to C# again. 我正在寻找将C#转换为XML并再次反序列化为C#的选项。 Anyone know of any existing solutions for this? 有人知道任何现有的解决方案吗? I assume a Roslyn SyntaxTree is not Xml-serializable. 我假设Roslyn SyntaxTree不是Xml可序列化的。 I also assume making a xml-serializer for a syntaxtree would be quite a large undertaking. 我还假设为一个syntaxtree制作一个xml-serializer是一项相当大的工作。 But maybee not? 但也许不是吗?

Why do I need this? 我为什么需要这个? I want to generate code from an xml-model via xsl to syntaxtree-xml and then to c# (or maybee vb). 我想通过xsl到syntaxtree-xml从xml模型生成代码,然后生成c#(或者可能是vb)。

I don't want to generate C# directly from XSL (can be done but I don't feel it's ideal). 我不想直接从XSL生成C#(可以做但我觉得它不是理想的)。 I need XSL because I'm also going to generate XAML and HTML from the XML-model and I'd like to also be able to generate any code behind needed etc. 我需要XSL,因为我还要从XML模型生成XAML和HTML,我也希望能够生成任何需要的代码等等。

I know about T4-templates but right now I'm looking at other options for various reasons. 我知道T4模板,但现在我正在寻找其他选项,原因有很多。

Roslyn is an open compiler. Roslyn是一个开放的编译器。 I assume you can straightforwardly insert your own code to walk over the AST and dump it in XML format. 我假设您可以直接插入自己的代码来遍历AST并以XML格式转储它。 Given that it is open, presumbly you can implement your own extension to read your dumped XML, and build a Roslyn AST (which I think can then be prettyprinted). 鉴于它开放的,因此你可以实现自己的扩展来读取你的转储XML,并构建一个Roslyn AST(我认为它可以被精心打印)。 That seems like a lot of work. 这似乎很多工作。

For a canned solution, see C# AST in XML . 对于固定解决方案,请参阅XML中的C#AST A similar read-XML-to-make-AST would be needed for that solution, too. 该解决方案也需要类似的读取XML-to-make-AST。

You might not want to use XML for trees; 您可能不希望将XML用于树; such files are generally enormous. 这样的文件通常是巨大的。 See What would an AST (abstract syntax tree) for an object-oriented programming language look like? 请参阅面向对象编程语言的AST(抽象语法树)是什么样的? .

It is likely better for you to operate within the context of a tool that parses and builds ASTs, do your manipulation directly on those ASTs, and then prettyprint the result. 您可能更好地在解析和构建AST的工具的上下文中操作,直接在这些AST上进行操作,然后对结果进行相同的打印。 Our DMS system has both parsing (shown by the previous two examples) and prettyprinting for many languages including C# already built-in, and many useful APIs for inspecting/modifying/generating code from the ASTs it constructs. 我们的DMS系统既有解析(前面两个例子所示),也有许多语言的漂亮打印,包括已经内置的C#,以及许多有用的API,用于检查/修改/生成它构建的AST代码。 (XSL is lot less useful for analyzing ASTs than you might think; it doesn't understand "context" [eg, the surrounding scopes and declarations] that give programs the meaning they have). (XSL对于分析AST的作用远不如你想象的那么有用;它不理解“上下文”[例如,周围的范围和声明]给程序赋予它们的意义)。

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

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