2012-12-05 72 views
1

我的需求是當構建發生T4模板應該執行的時候。
這是我在模板中編寫的代碼。使用構建過程的T4模板

<#@ template debug="false" hostspecific="true" language="C#" #> 
<#@ assembly name="System.Core" #> 

<#@ assembly name="$(SolutionDir)dll\Newtonsoft.Json.dll" #> 

<#@ import namespace="System.Linq" #> 
<#@ import namespace="System.Text" #> 
<#@ import namespace="System.Collections.Generic" #> 
<#@ import namespace="System.IO"#> 
<#@ import namespace="System.Text"#> 

<#@ import namespace="Newtonsoft.Json"#> 
<#@ import namespace="Newtonsoft.Json.Linq"#> 

<#@ output extension=".txt" #> 

<# 

string serverPath = this.Host.ResolvePath("DLLs.js"); 

string body; 
using (var stream = new FileStream(serverPath, FileMode.Open, FileAccess.Read)) 
{ 
    var streamReader = new StreamReader(stream); 
    body = streamReader.ReadToEnd(); 

} 

#> 

<#=body#> 

這運行正常,但是當我試圖建立它給一個例外,它無法找到Newtonsoft DLL。

這是我在項目文件中做的,在解決方案構建時運行模板。

<Import Project="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\TextTemplating\v11.0\Microsoft.TextTemplating.targets" /> 
<PropertyGroup> 
<TransformOnBuild>true</TransformOnBuild> 
</PropertyGroup> 

    <ItemGroup> 
    <T4ReferencePath Include="$(VsIdePath)PublicAssemblies\" /> 
    </ItemGroup> 

Newtonsoft.dll位於解決方案文件夾根目錄下的DLL文件夾中。

我看到帖子@Get Visual Studio to run a T4 Template on every build,也遵循@MarkGr建議的步驟,但沒有運氣。

我正在使用VS2012。有人可以告訴我我哪裏錯了嗎?

+0

你能告訴我們到底發生了什麼(和_didn't_發生什麼)當您嘗試這些指示? –

+0

當我生成我得到以下生成錯誤:錯誤主機嘗試解析程序集引用'$(SolutionDir)dll \ Newtonsoft.Json.dll'時引發異常。轉換不會運行。引發以下異常: System.IO.FileLoadException:給定的程序集名稱或代碼庫無效。 (從HRESULT異常:0x80131047)在System.Reflection.AssemblyName.nInit(運行時Assembly&assembly,布爾forIntrospection,布爾raiseResolveEvent) at System.Reflection.AssemblyName..ctor(String assemblyName) – Anirban

+0

嗯。看起來像一個64/32位問題。 –

回答