Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

T4 template and Assembly.Load

Tags:

c#

.net

t4

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.

like image 999
Kapitán Mlíko Avatar asked Aug 17 '12 07:08

Kapitán Mlíko


1 Answers

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

  • http://msdn.microsoft.com/en-us/library/ms172331(v=vs.110).aspx
like image 109
Bohdan Avatar answered Sep 30 '22 16:09

Bohdan