How to disable #line directives being written to the T4 generation output file

前端 未结 2 1748
执笔经年
执笔经年 2021-02-18 21:02

I have encountered a small problem with my T4 code generation.

I have broken my T4 templates up into separate files and placed them in various directories, I have done t

相关标签:
2条回答
  • 2021-02-18 21:17

    I'm not sure this will fix your problem. I don't have a great deal of experience with T4. That said, I'm using it build a number of files in my project and I have not run into the problem you are running into.

    In my case, I have a bunch of individual .tt files that render individual files in my project.

    The way I'm doing it is as follows: I have a SaveOutput method (stolen from somewhere on stackoverflow, I believe):

    void SaveOutput(string outputFileName)
    {
        if (!string.IsNullOrEmpty(outputFileName))
        {
            string templateDirectory = Path.GetDirectoryName(Host.TemplateFile);
            string outputFilePath = Path.Combine(templateDirectory, outputFileName);
            string output = this.GenerationEnvironment.ToString();
            File.WriteAllText(outputFilePath, output); 
        }
    
        this.GenerationEnvironment.Remove(0, this.GenerationEnvironment.Length);
    }
    

    So in my main .tt file, I have <#@ includes... #> for my various .tt files for the individual files. For example: <#@ include file="DataObjectInterface.tt" #>

    <#+
        public string GenerateInterfaceDataObject(sqlTable table)
        {
            string ifaceName = "I" + table.ObjectName;
            if (table.GenerateInterfaceObjectAsBase)
            {
                ifaceName += "Base";
            }
    #>
    using System;
    
    namespace <#= InterfaceNamespace #>
    {
        public interface <#= ifaceName #>
        {
    <#+
            foreach(sqlColumn col in table.Columns)
            {
    #>
            <#= GetColumnCSharpDataType(col) #> <#= col.FieldName #> { get; }
    <#+
            }
    #>
        }
    }
    <#+
            return RootPath + @"\AutoGenerated\" + ifaceName + ".cs";
        }
    #>
    

    Then, in my main .tt class, I simply make a call like this:

    string filePath = GenerateInterfaceDataObject(table);
    SaveOutput(filePath);
    

    Not sure if this method of breaking your templates up will work for your needs, but it worked for mine and I haven't run into any problems with it yet.

    I should clarify that my main .tt file doesn't actually generate anything from itself. It is not itself a template. It simply loads each of the templates and generates the files from the the other templates.

    0 讨论(0)
  • 2021-02-18 21:19

    Visual Studio 2012 adds the linePragmas="false" template directive:

    <#@ template language="C#" linePragmas="false" #>
    

    http://msdn.microsoft.com/en-us/library/gg586945(v=vs.110).aspx

    Still not sure how to do this in VS2010 which I'm stuck with at work.

    0 讨论(0)
提交回复
热议问题