.net 4.0 如何使用T4和XML生成VisualStudio项目?

.net 4.0 如何使用T4和XML生成VisualStudio项目?,.net-4.0,code-generation,t4,.net 4.0,Code Generation,T4,大家好 在早期,使用代码Dom是可能的,我知道使用T4比使用代码Dom容易得多。我看过很多文章 但我仍然无法起草出符合我要求的准则 我需要根据自动生成的XML文档生成代码[我能够根据自己的需求生成XML文档] 看起来是这样的: <?xml version="1.0" encoding="utf-8" ?> <GeneratedSolution Name ="name of Solution"> <GeneratedProject ID="RepoProjec

大家好

在早期,使用代码Dom是可能的,我知道使用T4比使用代码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>

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


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

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

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

在链接的博客上,我们描述了一种基于强定义输入数据(XML模式形式)控制生成输出的方法。我们在T4团队的博客中提到了这一点,以强调这是标准的T4用途:

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

对于经验丰富的T4用户,视频已经从零开始,并解释其用法。我正在通过简单的下载添加基本结构(对于git用户,可以通过git at获得)


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

谢谢Novakov-您的想法肯定是好的,但是使用Mono并不适合我们,也不是一个好主意。您不必使用整个Mono运行时,只需使用单个程序集。我在.NET4.0运行时成功地使用了Mono.TextTemplating。非常感谢!请提供一些链接或示例,以便我可以使用或从头开始。从克隆MonoDevelop存储库和编译TextTemplating程序集开始。非常感谢!我在博客上关注你们。我添加了你们案例的精确演示:非常值得欣赏,非常感谢。我需要分叉还是克隆它?祝你圣诞快乐:)