Antlr有一个example of a process可用于将生成的代码添加到项目。这具有显示嵌套在源文件下的文件的优点,尽管添加起来更复杂。
需要添加的项目组与待从所生成的文件,例如:
<ItemGroup>
<ServiceDescription Include="MyService.txt"/>
</ItemGroup>
然后添加要产生包含源代码的其余部分的的ItemGroup的CS文件。
<ItemGroup>
...
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
...etc..
<Compile Include="MyService.txt.cs">
<AutoGen>True</AutoGen>
<DesignTime>True</DesignTime>
<DependentUpon>MyService.txt</DependentUpon> <!--note: this should be the file name of the source file, not the path-->
</Compile>
</ItemGroup>
然后最后添加构建目标以执行代码生成(使用%为ItemGroup中的每个项目执行命令)。这可以放在一个单独的文件中,以便它可以包含在许多项目中。
<Target Name="GenerateService">
<Exec Command="servicegen.exe -i:%(ServiceDescription.Identity) -o:%(ServiceDescription.Identity).cs" />
</Target>
<PropertyGroup>
<BuildDependsOn>GenerateService;$(BuildDependsOn)</BuildDependsOn>
</PropertyGroup>