T4(文本模板转换工具包)用于根据 C# 中的基本类型列表(例如 PointI32、PointF32 等)生成一组类型
类似于 用于在 C++/CLI 中基于基本类型列表(例如 PointI32、PointF32 等)生成一组类型的 Boost 预处理器库 我问如何生成:
struct Point##TYPE_SUFFIX_NAME
{
TYPE X
{ get; set; }
TYPE Y;
{ get; set; }
// Other code
};
对于不同的基本 (POD) 数据类型,例如:
PointF32, PointF64, PointI32 etc.
使用 T4 ( Visual Studio 2008 或更高版本中的文本模板转换工具包)。
请参阅http://www.olegsych.com/2007/12/ text-template-transformation-toolkit/ 和 http://msdn .microsoft.com/en-us/library/bb126445.aspx
Similar to Boost Preprocessor library for generating a set of types based on a list of basic types e.g. PointI32, PointF32 etc. in C++/CLI I am asking how to generate:
struct Point##TYPE_SUFFIX_NAME
{
TYPE X
{ get; set; }
TYPE Y;
{ get; set; }
// Other code
};
for different basic (POD) data types e.g.:
PointF32, PointF64, PointI32 etc.
using T4 (Text Template Transformation Toolkit) in Visual Studio 2008 or later.
See http://www.olegsych.com/2007/12/text-template-transformation-toolkit/ and http://msdn.microsoft.com/en-us/library/bb126445.aspx
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,我自己有答案。我创建了以下 T4 包含文件:
SignedIntegersSuffices.ttinclude
UnsignedIntegersSuffices.ttinclude
IntegersSuffices.ttinclude
FloatsSuffices.ttinclude
,然后实际的 T4 模板是
PointTypes.tt
。这些文件只需添加到 C# 项目中,每当保存 .tt 文件时,Visual Studio 就会检测.tt
并生成匹配的PointTypes.cs
文件。下面列出了这些文件。
SignedIntegersSuffices.ttinclude
UnsignedIntegersSuffices.ttinclude
IntegersSuffices.ttinclude
FloatsSuffices.ttinclude
BasicTypesSuffices.ttinclude
最后是实际的模板文件
PointTypes.tt
:输出文件
PointTypes.cs
将如下所示:非常简单且非常有效。当然,现在这可以使用泛型等来完成,但这不是这里的重点。这是多种基本类型的代码生成的简单示例。
欢迎提出改进或类似的意见。
Well, I have the answer my self. I have created the following T4 include files:
SignedIntegersSuffices.ttinclude
UnsignedIntegersSuffices.ttinclude
IntegersSuffices.ttinclude
FloatsSuffices.ttinclude
BasicTypesSuffices.ttinclude
and then the actual T4 template is
PointTypes.tt
. These files are simply added to a C# project and Visual Studio will detect the.tt
and generate a matchingPointTypes.cs
file, whenever the .tt file is saved.These files are listed in the following.
SignedIntegersSuffices.ttinclude
UnsignedIntegersSuffices.ttinclude
IntegersSuffices.ttinclude
FloatsSuffices.ttinclude
BasicTypesSuffices.ttinclude
And finally the actual template file
PointTypes.tt
:The output file
PointTypes.cs
will then look like this:Pretty simple and quite effective. Now, of course, this could have been done using generics etc, but that is not the point here. This is meant as a simple example of code generation for multiple basic types.
Comments to improve this or similar are welcomed.