2011-12-18 96 views
0

大家好!如何使用XML使用T4生成Visual Studio项目?

在早期就可以使用代码-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> 

的类号,项目可能单个溶液内变化。 我读了一些文章,提供了一个想法,从XML创建项目,但我需要更多的东西来生成代码,如上面的'XML'样本。

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

回答

1

免责声明 - 我们发现的方法学,但完全开放源代码。

在链接博客我们所描述的方式基于强烈定义的输入数据的生成输出控制(XML模式的形式)。我们得到了通过T4团队的博客一提 - 强调的是,它是唯一的标准T4使用:

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

对于T4:

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

链接到博客的视频可以直接通过到达经验丰富的用户视频已经从头开始并解释使用情况。我正在通过简单的下载来添加基本结构(对于git用户,它可以通过git在https://github.com/abstractiondev/absInitializer处获得)。

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

+0

非常感谢!我正在关注你的博客。 – 2011-12-20 18:19:15

+1

我添加了您的案例精确演示:https://github.com/abstractiondev/DemoSOCase8552428ABS – Kallex 2011-12-22 22:22:27

+0

真的很可观,非常感谢。我需要分叉还是克隆它?祝你MERRY X'MASS :) – 2011-12-23 14:13:56

0

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

+0

感谢Novakov - 绝对是你的想法是好的,但使用Mono是不是对我们合适的或好主意 – 2011-12-18 14:45:57

+0

您不必使用全单运行时,只是单一组装。我使用了Mono.TextTemplating,并在.NET 4.0运行时上取得了成功。 – Novakov 2011-12-18 23:02:09

+0

非常感谢!您能否提供一些链接或示例,以便我可以使用或从头开始。 – 2011-12-20 17:44:19

相关问题