6

This is my first question on StackOverflow so Hi :)

Is it possible to load assembly by Assembly name using Assembly.Load() in t4 template? I would like to use it to get all types with ServiceContract attribute in loaded assembly.

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

Desired Assembly is referenced in project where my template is. I figured out that

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

works but it does not seems like good solution. Besides I want that template to transform after build and when I add following lines to .csproj

      <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>

solution with Assembly.GetAssembly does not work either. Thank you for all suggestions.

Kapitán Mlíko
  • 4,498
  • 3
  • 43
  • 60
  • Have you seen this? http://stackoverflow.com/questions/3434713/cant-reference-an-assembly-in-a-t4-template – devlife Jan 07 '13 at 23:21
  • Oh wow! Your case is exactly the same as mine, down to the wcf attribute check! Did you solve this? I'm trying to load an assembly manually using ShadowCopy to prevent locking issues on the dlls used inside the T4 template. – julealgon Dec 13 '13 at 20:46
  • @julealgon unfortunately no. I do it manually whenever I know that the outcome changes. – Kapitán Mlíko Dec 15 '13 at 15:46

2 Answers2

1

Did you try to load assembly to reflection-only context?

Bohdan
  • 1,987
  • 16
  • 23
0

I had the same issue using the Microsoft.TextTemplating.targets directive. You can add the transform command at the end of the .csproj as a possible workaround:

<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>
xmamat
  • 181
  • 5