如何以编程方式控制从T4生成的新文件的Build Action?

And*_*rei 7 .net c# t4 templates

问题是:有没有办法在*.tt文件中设置一个设置,以便生成的文件设置为指定的Build Action?

问题是我使用模板生成代码,但它只是一个起点,消除了大量的输入.我不希望任何人按原样使用代码(生成的类),也不希望它混淆命名空间.目前,每次模板添加新文件时,我都必须手动将Build Action设置为None - 我想自动化它.

谢谢!

Dáv*_*nár 1

一个非常简单的解决方法是将生成的代码包装到#if语句中:

#if GENERATED_CODE
    // my generated code
    // will compile only if the variable GENERATED_CODE is defined
#endif
Run Code Online (Sandbox Code Playgroud)