How to make TT T4 template output multiple files (VS2010)-access911.net article

Source: Internet
Author: User
Tags parent directory

How to make TT T4 template output multiple files (VS2010)-access911.net article

Problem:

The TT template feature in VS2010 is very useful, but how to define its output directory, or how to program a TT file to output multiple result files.

Reply:


<#@ Import namespace= "System.Text" #>
<#@ Import namespace= "System.IO" #>
<#@ Import namespace= "System.Reflection" #>


<#+
<summary>
This class is primarily used to redefine the output position of the TT T4 so that its output position can be dynamically defined in the T4.
</summary>
<remarks>
If you want to use this class, you need to refer to the following two files in your project
C:\WINDOWS\Microsoft.NET\assembly\GAC_MSIL\Microsoft.VisualStudio.TextTemplating.10.0\v4.0_10.0.0.0__ B03f5f7f11d50a3a\microsoft.visualstudio.texttemplating.10.0.dll
C:\WINDOWS\Microsoft.NET\assembly\GAC_MSIL\Microsoft.VisualStudio.TextTemplating.Interfaces.10.0\v4.0_ 10.0.0.0__b03f5f7f11d50a3a\microsoft.visualstudio.texttemplating.interfaces.10.0.dll
</remarks>
<example>
<#@ Include file= "Classfile.ttinclude" #>
using (New File (FullPath, this))
///    {
Write the template code here
/// }
</example>
public class File:System.IDisposable
{
private string M_filename;
private string m_buffercontent;
Private StringBuilder M_textbuffer;

<summary>
Here Texttransformation texttransformation always prompt for error 5 could not find the type or namespace name "Texttransformation" (is missing a using directive or assembly reference?) C:\xheditor-1.1.14\demo_ASP4\demo_ASP4\T4\ClassFile.ttinclude 22 33
But still can run normally, do not know why.
Finally found the Microsoft.VisualStudio.TextTemplating class, in C:\WINDOWS\Microsoft.NET\assembly\GAC_MSIL\ Microsoft.visualstudio.texttemplating.10.0\v4.0_10.0.0.0__b03f5f7f11d50a3a\ The Microsoft.visualstudio.texttemplating.10.0.dll file.
The Texttransformation parameter defines a namespace whose type is generatedtexttransformation or texttransformation can be successfully generated because it is never possible with a randomly generated name in the TT template. T The exttransformation corresponds to.
Also need to reference C:\WINDOWS\Microsoft.NET\assembly\GAC_MSIL\Microsoft.VisualStudio.TextTemplating.Interfaces.10.0\v4.0_ 10.0.0.0__b03f5f7f11d50a3a\microsoft.visualstudio.texttemplating.interfaces.10.0.dll
</summary>
<param name= "FileName" ></param>
<param name= "Texttransformation" ></param>
Public File (String fileName, Generatedtexttransformation texttransformation)
Public File (String fileName, Generatedtexttransformation texttransformation)
{

string outputDir = null;
The original code on the Internet is as follows;
PropertyInfo Outputdirprop = Texttransformation.gettype (). GetProperty ("OutputDirectory");
if (Outputdirprop! = null)
OutputDir = (string) outputdirprop.getvalue (texttransformation, NULL);

M_filename = (outputDir?? "") + FileName;

         //The parent directory to identify the error, so simply define the file name including the directory name that is done by the parent program
          m_fileName = fileName;
                                                                                                                                        

         PropertyInfo genEnvProp =  Texttransformation.gettype (). GetProperty ("Generationenvironment",  bindingflags.flattenhierarchy| bindingflags.nonpublic| BindingFlags.Instance);
         if  (genenvprop != null)
          {
             m_textBuffer =  (StringBuilder) genenvprop.getvalue (texttransformation, null);
            m_buffercontent = m_ Textbuffer.tostring ();
            m_textbuffer.remove (0, m_ Textbuffer.length);
         }
         return;

}

#region IDisposable Members
public void Dispose ()
{
if (m_textbuffer! = null)
{
using (StreamWriter StreamWriter = new StreamWriter (m_filename))
{
StreamWriter.Write (M_textbuffer.tostring ());
}
M_textbuffer.remove (0, m_textbuffer.length);
M_textbuffer.append (m_buffercontent);
}
Return
}
#endregion
}


#>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.