T4 and Assembly.Load

This is my first question about StackOverflow, so Hi :)

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

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

The desired assembly is referenced in the project where my template is located. I realized that

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

works, but it doesn't seem like a good solution. Also, I want this template to be converted after the build, and when I add the 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>

with Assembly.GetAssembly also does not work. Thank you for all the suggestions.

+5
source share
2 answers

Have you tried loading the assembly in a context for reflection only?

0

, Microsoft.TextTemplating.targets. transform .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>
0

All Articles