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.
Did you try to load assembly to reflection-only context?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With