T4 Text Template - Is it possible to get compilation symbols from host?

后端 未结 1 1432
遇见更好的自我
遇见更好的自我 2021-01-18 08:58

Background:

I have an open source game library written in C# which is highly tuned for performance, using unsafe code, pointer arithmetic etc. I\'ve

相关标签:
1条回答
  • 2021-01-18 09:32

    Matt, They're not available, no as the compilation for your project and the compilation for the templates are completely isolated as of VS2010.

    However I have a (somewhat clumsy) workaround for you.

    If you include a stub file that contains your template directive, then you can set compiler options with the appropriate symbol for compilation.

    e.g. a simple stub containing the following, named Define.t4:

    <#@ template compilerOptions="/d:XBOX" debug="false" hostspecific="false" language="C#" #>
    

    Put this in a subdirectory named XBOX and put a similar one in a directory named PHONE

    <#@ template compilerOptions="/d:PHONE" debug="false" hostspecific="false" language="C#" #>
    

    Note the compiler symbol definitions.

    Now set an environment variable to be either XBOX or PHONE - let's say ARCHITECTURE

    Now instead of the template directive in your templates, include the Define.t4 file and you will be able to use regular C#/VB condition syntax. Don't forget to put the #if on their own line. Here's an example:

    <#@ include file="$(ProjectDir)%ARCHITECTURE%\Define.t4" #>
    <#@ output extension=".txt" #>
    <#
    #if XBOX #>
    public unsafe struct Foo
    <#
    #else #>
    public struct Foo
    <#
    #endif #>
    

    Hope this gets you moving.

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