如何使用 T4 使用 XML 生成 Visual Studio 项目?

发布于 2024-12-22 04:12:10 字数 2613 浏览 4 评论 0 原文

大家好!

在早期,可以使用Code-Dom,我开始知道使用T4比Code-Dom容易得多。我浏览了很多文章 但我仍然无法起草满足我的要求的代码。

我需要根据自动生成的 XML 文档生成代码 [我能够根据我的要求生成 XML 文档]。

看起来像这样:

    <?xml version="1.0" encoding="utf-8" ?>
<GeneratedSolution Name ="name of Solution">
<GeneratedProject ID="RepoProject" Name ="name of Project" MajorVersion="1" MinorVersion="0" CopyRight="GeneratedProject company" Language="C#" OutExtension="cs">
<Compiler path = "../path/compilername" Build="silent" ExternalBuildTool="NAnt" />

<GeneratedRepository Name ="name of Repository Class" Description="This is generated Repository class would be used for overall-application" TemplateRefence ="../path/repository.tt">
<Operation Name = "Operation1" Description = "This is operation1 of Repository class">
<operationID>1</operationID>
</Operation>
<Operation Name = "Operation2" Description = "This is operation2 of Repository class">
<operationID>2</operationID>
</Operation>
</GeneratedRepository>

</GeneratedProject>

<GeneratedProject ID="BusinessProject" Name ="name of Project" MajorVersion="1" MinorVersion="0" CopyRight="GeneratedProject company">

<GeneratedBusiness Name ="name of Business Class" Description="This is generated Business class would be used for overall-application" TemplateRefence ="../path/business.tt">
<Operation Name = "Operation1" Description = "This is operation1 of Business class">
<operationID>1</operationID>
</Operation>
<Operation Name = "Operation2" Description = "This is operation2 of Business class">
<operationID>2</operationID>
</Operation>
</GeneratedBusiness>

</GeneratedProject>

<GeneratedProject ID="UIProject" Name ="name of Project" MajorVersion="1" MinorVersion="0" CopyRight="GeneratedProject company">

<GeneratedUI Name ="name of UI Class" Description="This is generated UI class would be used for overall-application" TemplateRefence ="../path/UI.tt">
<Operation Name = "Operation1" Description = "This is operation1 of UI class">
<operationID>1</operationID>
</Operation>
<Operation Name = "Operation2" Description = "This is operation2 of UI class">
<operationID>2</operationID>
</Operation>
</GeneratedUI>

</GeneratedProject>

</GeneratedSolution>

单个解决方案中的类、项目数量可能会有所不同。 我阅读了一些文章,其中提供了从 xml 创建项目的想法,但我需要更多内容来根据上面的“XML”示例生成代码。

在这方面的任何帮助将不胜感激。

Hi all!

In earlier days it is possible using Code-Dom, I came to know that using T4 is much easier than Code-Dom. I have gone through many articles
but still I am not in a situation to draft-out the code which will meet my requirements.

I need to generate a code on the basis of an auto-generated XML document [I am able to generate the XML document as per my requirements].

It seems like this :

    <?xml version="1.0" encoding="utf-8" ?>
<GeneratedSolution Name ="name of Solution">
<GeneratedProject ID="RepoProject" Name ="name of Project" MajorVersion="1" MinorVersion="0" CopyRight="GeneratedProject company" Language="C#" OutExtension="cs">
<Compiler path = "../path/compilername" Build="silent" ExternalBuildTool="NAnt" />

<GeneratedRepository Name ="name of Repository Class" Description="This is generated Repository class would be used for overall-application" TemplateRefence ="../path/repository.tt">
<Operation Name = "Operation1" Description = "This is operation1 of Repository class">
<operationID>1</operationID>
</Operation>
<Operation Name = "Operation2" Description = "This is operation2 of Repository class">
<operationID>2</operationID>
</Operation>
</GeneratedRepository>

</GeneratedProject>

<GeneratedProject ID="BusinessProject" Name ="name of Project" MajorVersion="1" MinorVersion="0" CopyRight="GeneratedProject company">

<GeneratedBusiness Name ="name of Business Class" Description="This is generated Business class would be used for overall-application" TemplateRefence ="../path/business.tt">
<Operation Name = "Operation1" Description = "This is operation1 of Business class">
<operationID>1</operationID>
</Operation>
<Operation Name = "Operation2" Description = "This is operation2 of Business class">
<operationID>2</operationID>
</Operation>
</GeneratedBusiness>

</GeneratedProject>

<GeneratedProject ID="UIProject" Name ="name of Project" MajorVersion="1" MinorVersion="0" CopyRight="GeneratedProject company">

<GeneratedUI Name ="name of UI Class" Description="This is generated UI class would be used for overall-application" TemplateRefence ="../path/UI.tt">
<Operation Name = "Operation1" Description = "This is operation1 of UI class">
<operationID>1</operationID>
</Operation>
<Operation Name = "Operation2" Description = "This is operation2 of UI class">
<operationID>2</operationID>
</Operation>
</GeneratedUI>

</GeneratedProject>

</GeneratedSolution>

No. of classes, projects may vary within single solution.
I read some articles which provide an idea to create project from xml but I need something more to generate code as per above sample of 'XML'.

Any help in this regards will be much appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

初见 2024-12-29 04:12:10

免责声明 - 我们发现的方法但完全开源。

在链接的博客上,我们描述了一种基于强定义的输入数据(以 XML 模式形式)控制生成输出的方法。我们通过 T4 团队的博客提及 - 强调它仅适用于标准 T4:

http://blogs.msdn.com/b/t4/archive/2011/11/30/some-nice-new-getting-started-with-t4-videos.aspx

可以通过以下方式直接访问博客视频的链接:

http://abstractiondev.wordpress.com/demo-videos/

对于 T4 经验丰富的用户,视频已经从头开始并解释了用法。我正在通过简单的下载添加基本结构(对于 git 用户,可以通过 git 在 https://github.com 获取。 com/abstractiondev/absInitializer)。

如果您在这方面需要任何帮助,请通过该博客上的联系信息给我留言,如果您喜欢我们的方法论并希望将其用于您这一代,我将为您“抽象”它。

Disclaimer - our discovered methodology but completely open source.

On the linked blog we are describing a way to control your generation output based on strongly-defined input data (in XML schema form). We got a mention through T4 Team's Blog - to underline that it is standard T4 use only:

http://blogs.msdn.com/b/t4/archive/2011/11/30/some-nice-new-getting-started-with-t4-videos.aspx

The link to blog's videos can be reached directly through:

http://abstractiondev.wordpress.com/demo-videos/

For T4 seasoned users the videos already start from scratch and explain the usage. I am in process of adding the basic structure through simple download (for git users it's available through git at https://github.com/abstractiondev/absInitializer).

If you need any assistance on that, just drop me a line through the contact info on that blog and I'll "abstract" it for you, if you like our methodology approach and want to use that for your generation.

壹場煙雨 2024-12-29 04:12:10

您可以使用 Mono.TextTemplate(MonoDevelop 的一部分)从您自己的应用程序运行 T4 模板并生成您想要的代码的任何部分。如果您不喜欢单独应用程序的想法,可以为 Visual Studio 编写自定义代码工具,它将运行您需要的所有转换。

You can use Mono.TextTemplating (part of MonoDevelop) to run T4 templates from your own app and generate any part of code you want. If you don't like the idea of separate app, it is possible to write custom code tool for visual studio which will run all transformations you need.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文