zoukankan      html  css  js  c++  java
  • 《如何让TT T4模板输出多个文件(VS2010中)》-- access911.net 文章

    《如何让TT T4模板输出多个文件(VS2010中)》-- access911.net 文章

     

    问题:

     VS2010中自带的 TT 模板功能挺好用,但是如何定义其输出的目录,或者如何编程让一个tt文件可以输出多个结果文件。

     

     

    回答:

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

     


    <#+
    /// <summary>
    /// 这个类主要用于重定义 TT t4 的输出位置,这样就可以在t4中动态定义其输出位置了。
    /// </summary>
    /// <remarks>
    /// 如果要使用这个类,需要在项目中引用以下两个文件
    /// C:WINDOWSMicrosoft.NETassemblyGAC_MSILMicrosoft.VisualStudio.TextTemplating.10.0v4.0_10.0.0.0__b03f5f7f11d50a3aMicrosoft.VisualStudio.TextTemplating.10.0.dll
    /// C:WINDOWSMicrosoft.NETassemblyGAC_MSILMicrosoft.VisualStudio.TextTemplating.Interfaces.10.0v4.0_10.0.0.0__b03f5f7f11d50a3aMicrosoft.VisualStudio.TextTemplating.Interfaces.10.0.dll
    /// </remarks>
    /// <example>
    /// <#@ Include File="ClassFile.ttinclude" #>
    /// using (new File(fullPath, this))
    ///    {
    ///     //这里写模板代码
    /// }
    /// </example>

       public class File : System.IDisposable
       {
          private string m_fileName;
          private string m_bufferContent;
          private StringBuilder m_textBuffer;

          /// <summary>
          /// 这里 TextTransformation textTransformation 一直提示 错误    5    未能找到类型或命名空间名称“TextTransformation”(是否缺少 using 指令或程序集引用?)    C:xheditor-1.1.14demo_ASP4demo_ASP4T4ClassFile.ttinclude    22    33    
          /// 但是仍然能够正常运行,不知道为什么。
          /// 终于找到 Microsoft.VisualStudio.TextTemplating 类了,在 C:WINDOWSMicrosoft.NETassemblyGAC_MSILMicrosoft.VisualStudio.TextTemplating.10.0v4.0_10.0.0.0__b03f5f7f11d50a3aMicrosoft.VisualStudio.TextTemplating.10.0.dll 文件中。
          /// textTransformation 参数定义其类型为  GeneratedTextTransformation 或者  TextTransformation 都可以成功生成,因为它永远不可能与 tt 模板中随机生成的   名称空间.TextTransformation  对应上。
          /// 还需要引用 C:WINDOWSMicrosoft.NETassemblyGAC_MSILMicrosoft.VisualStudio.TextTemplating.Interfaces.10.0v4.0_10.0.0.0__b03f5f7f11d50a3aMicrosoft.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;
             //网上的原始代码如下;
             //PropertyInfo outputDirProp = textTransformation.GetType().GetProperty("OutputDirectory");
             //if (outputDirProp != null)
             //   outputDir = (string)outputDirProp.GetValue(textTransformation, null);
                                                         
             //m_fileName = (outputDir ?? "") + fileName;

             //父目录识别错误,所以干脆定义文件名包括目录名由父程序来完成
             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
       }


    #>

  • 相关阅读:
    Max Sum Plus Plus HDU
    Monkey and Banana HDU
    Ignatius and the Princess IV HDU
    Extended Traffic LightOJ
    Tram POJ
    Common Subsequence HDU
    最大连续子序列 HDU
    Max Sum HDU
    畅通工程再续
    River Hopscotch POJ
  • 原文地址:https://www.cnblogs.com/lexus/p/3728970.html
Copyright © 2011-2022 走看看