2012-08-17 166 views
5

這是我在計算器上的第一個問題這麼嗨:)T4模板和Assembly.Load

是否有可能在T4模板使用Assembly.Load()彙編名加載程序集? 我想用它來獲取加載程序集中帶有ServiceContract屬性的所有類型。

var loadedAssembly = Assembly.Load(assemblyName); 
    var types = from type in loadedAssembly.GetTypes() 
    where Attribute.IsDefined(type, typeof(ServiceContractAttribute))select type; 

所需的程序集在我的模板所在的項目中被引用。我想通過

<#@ assembly name="$(TargetDir)\DesiredAssemblyName.dll" #> 
    var loadedAssembly = Assembly.GetAssembly(typeof(SomeType)); 

的作品,但它似乎不是很好的解決方案。此外,我想該模板生成後改變,當我添加以下行的.csproj與Assembly.GetAssembly

 <Import Project="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\ 
     TextTemplating\v10.0\Microsoft.TextTemplating.targets"/> 
     <PropertyGroup> 
     <TransformOnBuild>true</TransformOnBuild> 
     </PropertyGroup> 
     <ItemGroup> 
     <!--Add VS\...\PublicAssemblies to the list of places 
     to look for assemblies used by templates.--> 
     <T4ReferencePath Include="..\Onii.Vespa.AppServer\"/> 
     </ItemGroup> 

解決方案也不起作用。 謝謝你的所有建議。

+0

你見過這個嗎? http://stackoverflow.com/questions/3434713/cant-reference-an-assembly-in-a-t4-template – devlife 2013-01-07 23:21:38

+0

哦哇!你的情況和我的完全一樣,一直到wcf屬性檢查!你解決了這個問題嗎?我正在嘗試使用ShadowCopy手動加載程序集,以防止在T4模板中使用的dll上出現鎖定問題。 – julealgon 2013-12-13 20:46:42

+0

@julealgon不幸的沒有。只要我知道結果發生變化,我就會手動完成。 – 2013-12-15 15:46:59

回答

0

我不得不使用Microsoft.TextTemplating.targets指令相同的問題。您可以在.csproj的末尾添加變換命令作爲可能的解決方法:

<Target Name="AfterBuild"> 
    <Exec Command="&quot;%CommonProgramFiles(x86)%\Microsoft Shared\TextTemplating\$(VisualStudioVersion)\TextTransform&quot; -P &quot;..\Onii.Vespa.AppServer\&quot; -I &quot;$(ProjectDir.TrimEnd('\'))&quot; YourTemplate.tt" /> 
</Target>