All,
I have a T4 Template
<#@ template debug="true" hostSpecific="true" #>
<#@ output extension=".cs" #>
<#@ Assembly Name="System.Core" #>
<#@ Assembly Name="System.Windows.Forms" #>
<#@ import namespace="System" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="System.Diagnostics" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Collections" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="MyLibrarysRootNameSpace.SomeNamespace" #>
/*Rest of template follows*/
I'm trying to get the last line to import so that I can easily resuse this template in other projects, but I seem to be missing something. Is what I am trying to do possible? If so, how?
Import just adds a using statement, it does not reference the assembly. T4's referenced assembly set is completely divorced from the project that is hosting the template.
If you want to bring in your hosting project's assembly then you need an assembly directive to do it. Something like the following:
<#@ assembly name="$(TargetPath)" #>
Note that you are introducing a build loop here, so the project will need manual intervention to build until it has produced a DLL once, so make sure you only generate partials that are optional or can always use the previous checked in version.
If I understood your question correctly:
Copy-paste the first 2 snippets from there to get the EnvDTE object model for the project that contains the T4:
<#@ assembly name="EnvDte" #>
<#
var visualStudio = ( this.Host as IServiceProvider )
.GetService( typeof( EnvDTE.DTE ) ) as EnvDTE.DTE;
var project = visualStudio.Solution
.FindProjectItem( this.Host.TemplateFile )
.ContainingProject as EnvDTE.Project;
#>
Then, use the method from there to obtain the default namespace of that project:
// project is of type: EnvDTE.Project
string strDefaultNamespace = project.Properties.Item( "DefaultNamespace" )
.Value.ToString();
Then, use strDefaultNamespace
value however your like.
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