2011-12-18 97 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

相關問題