Generating F# code

前端 未结 4 1684
春和景丽
春和景丽 2021-02-06 01:58

T4 is the \"official\" code generation engine for C#/VB.NET. But F# doesn\'t support it (this is from April, but I couldn\'t find any newer mentions). So what is a good way to g

4条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-06 02:25

    Because F# doesn't support custom tools in solution explorer, you can place your T4 files in a C# or Visual Basic project and redirect their output to your F# project. Here is how you can do it with T4 Toolbox:

    <#@ template language="C#" hostspecific="True" debug="True" #>
    <#@ output extension="txt" #>
    <#@ include file="T4Toolbox.tt" #>
    <#
        FSharpTemplate template = new FSharpTemplate();
        template.Output.Project = @"..\Library1\Library1.fsproj";
        template.Output.File = "Module2.fs";
        template.Render();
    #>
    <#+
    class FSharpTemplate: Template
    {
        public override string TransformText()
        {
    #>
    // Learn more about F# at http://fsharp.net
    
    module Module2
    <#+
            return this.GenerationEnvironment.ToString();
        }
    }
    
    #>
    

提交回复
热议问题