T4文本模板 - 是否有可能从主机获得编译符号? [英] T4 Text Template - Is it possible to get compilation symbols from host?

查看:112
本文介绍了T4文本模板 - 是否有可能从主机获得编译符号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景:

我有一个开源的游戏库写在C#这是非常适应性能,使用不安全code,指针运算等我最近移植库到Windows Phone。不幸的Windows Phone不支持不安全code可言,所以我不得不垃圾​​我的源$ C ​​$ C以preprocessor指令,像这样的:

I have an open source game library written in C# which is highly tuned for performance, using unsafe code, pointer arithmetic etc. I've recently ported the library to Windows Phone. Unfortunately Windows Phone does not support unsafe code at all, so I have had to litter my source code with preprocessor directives such as this:

#if WINDOWS || XBOX
public unsafe struct Foo
#elif WINDOWS_PHONE
public struct Foo
#endif

由于这些指令的codeBase的已经变得相当不可读和难以维持的量,所以我一直在研究其他方案 - 其中之一就是使用T4文本模板

Due to the amount of these directives the codebase has become quite unreadable and difficult to maintain, so I've been exploring other options - one of which is text templates using T4.

的问题:

我需要知道才去太远的T4途径是用是否有可能在一个文本模板,让编译符号?我研究过模板参数,但它似乎并不很容易从外界修改它们。理想的情况是什么,我想看到​​的东西有点像这样:

What I need to know before going too far down the T4 route is wether or not it is possible to get compilation symbols from within a text template? I have looked into template parameters but it didn't seem very easy to modify them from the outside world. Ideally what I'd like to see is something a bit like this:

<# if (Host.CompilationSymbols.Contains("WINDOWS_PHONE") { #>
public struct Foo
<# { #>

如果任何人都可以揭示阉一些轻或没有,这是可能的,我会感激不尽!

If anyone could shed some light on wether or not this is possible I'd be grateful!

推荐答案

马特, 他们不提供,不作为编译为您的项目和汇编的模板是完全隔离的VS2010的。

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.

例如。一个简单的存根包含以下内容,名为Define.t4:

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

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

将这个在一个子目录名为XBOX,放在一个名为电话类似的一个

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.

现在设置环境变量是无论是XBOX或电话 - 让我们说建筑

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

现在,包括Define.t4文件,你将能够使用常规的C#/ VB条件的语法。不要忘了把#如果自己行。这里有一个例子:

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.

这篇关于T4文本模板 - 是否有可能从主机获得编译符号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆