<#@ template language="C#" #>
<#@ output extension=".cs" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System.Linq" #>
<#
Type[] types_to_generate = new[]
{
typeof(object), typeof(bool), typeof(byte),
typeof(char), typeof(decimal), typeof(double),
typeof(float), typeof(int), typeof(long),
typeof(sbyte), typeof(short), typeof(string),
typeof(uint), typeof(ulong), typeof(ushort)
};
#>
using System;
public static class GreaterTest
{
<#
foreach (var type in types_to_generate)
{
#>
public static <#= type.Name #> of(<#= type.Name #> left, <#= type.Name #> right)
{
<#
Type icomparable =
(from intf in type.GetInterfaces() where
typeof(IComparable<>)
.MakeGenericType(type).IsAssignableFrom(intf)
||
typeof(IComparable).IsAssignableFrom(intf)
select intf).FirstOrDefault();
if (icomparable != null)
{
#>
return left.CompareTo(right) < ? right : left;
<#
}
else
{
#>
throw new ApplicationException(
"Type <#= type.Name #> must implement one of the " +
"IComparable or IComparable<<#= type.Name #>> interfaces.");
<#
}
#>
}
<#
}
#>
}

生成的代码:

using System;
public static class GreaterTest
{
public static Object of(Object left, Object right)
{
throw new ApplicationException(
"Type Object must implement one of the " +
"IComparable or IComparable<Object> interfaces.");
}
public static Boolean of(Boolean left, Boolean right)
{
return left.CompareTo(right) < ? right : left;
}
public static Byte of(Byte left, Byte right)
{
return left.CompareTo(right) < ? right : left;
}
public static Char of(Char left, Char right)
{
return left.CompareTo(right) < ? right : left;
}
public static Decimal of(Decimal left, Decimal right)
{
return left.CompareTo(right) < ? right : left;
}
public static Double of(Double left, Double right)
{
return left.CompareTo(right) < ? right : left;
}
public static Single of(Single left, Single right)
{
return left.CompareTo(right) < ? right : left;
}
public static Int32 of(Int32 left, Int32 right)
{
return left.CompareTo(right) < ? right : left;
}
public static Int64 of(Int64 left, Int64 right)
{
return left.CompareTo(right) < ? right : left;
}
public static SByte of(SByte left, SByte right)
{
return left.CompareTo(right) < ? right : left;
}
public static Int16 of(Int16 left, Int16 right)
{
return left.CompareTo(right) < ? right : left;
}
public static String of(String left, String right)
{
return left.CompareTo(right) < ? right : left;
}
public static UInt32 of(UInt32 left, UInt32 right)
{
return left.CompareTo(right) < ? right : left;
}
public static UInt64 of(UInt64 left, UInt64 right)
{
return left.CompareTo(right) < ? right : left;
}
public static UInt16 of(UInt16 left, UInt16 right)
{
return left.CompareTo(right) < ? right : left;
}
}

C# Meta Programming - Let Your Code Generate Code - Introduction of The Text Template Transformation Toolkit(T4)的更多相关文章

  1. C# Meta Programming - Let Your Code Generate Code - 利用反射重写自动的ToString()

    我们在写一些Model的时候,经常会重写ToString,为了在控制台中进行打印或者更好的单元测试. 但是,如果Model的字段非常多的时候,如此简单的重复劳动经常会变成一件令人头痛的事情,因为大家 ...

  2. 问题:Custom tool error: Failed to generate code for the service reference 'AppVot;结果:添加Service Reference, 无法为服务生成代码错误的解决办法

    添加Service Reference, 无法为服务生成代码错误的解决办法 我的解决方案是Silverlight+WCF的应用,Done Cretiria定义了需要在做完Service端的代码后首先运 ...

  3. Python 与 meta programming

    meta programming: 编写能改变语言语法特性或者运行时特性的程序 Meta- 这个前缀在希腊语中的本意是「在…后,越过…的」,类似于拉丁语的 post-,比如 metaphysics 就 ...

  4. Domain=com.alamofire.error.serialization.response Code=-1016 "Request failed: unacceptable content-type: text/html"

    2015-11-16 10:39:17.235 PullDemo[338:60b] Application windows are expected to have a root view contr ...

  5. AFNetworking 遇到错误 Code=-1016 "Request failed: unacceptable content-type: text/plain"

    在开发过程使用了AFNetworking库,版本2.x,先运行第一个官方例子(替换GET 后面的url即可): AFHTTPRequestOperationManager *manager = [AF ...

  6. Fortify漏洞之Dynamic Code Evaluation: Code Injection(动态脚本注入)和 Password Management: Hardcoded Password(密码硬编码)

    继续对Fortify的漏洞进行总结,本篇主要针对  Dynamic Code Evaluation: Code Injection(动态脚本注入) 和 Password Management: Har ...

  7. 解决 VS Code「Code Runner」插件运行 python 时的中文乱码问题

    描述 这里整理了两种 VS Code「Code Runner」插件运行 python 时乱码的解决方案.至于设置「Auto Guess Encoding」为 true 的操作这里就不多描述了. 乱码截 ...

  8. 在vue中获取微信支付code及code被占用问题的解决?

    这个地方坑比较多,查看网上并没有详细的文档,新手一般写到这里很痛苦.这里我只介绍一下我解决的方案,虽然它不是最好的,但是可行的方案: 总体分两步 1)跳到微信支付链接,它会自动拼接上code 2)获取 ...

  9. Fortify Audit Workbench 笔记 Dynamic Code Evaluation: Code Injection

    Dynamic Code Evaluation: Code Injection Abstract 在运行时中解析用户控制的指令,会让攻击者有机会执行恶意代码. Explanation 许多现代编程语言 ...

随机推荐

  1. Python urllib2 设置超时时间并处理超时异常

    可以使用 except: 捕获任何异常,包括 SystemExit 和 KeyboardInterupt,不过这样不便于程序的调试和使用 最简单的情况是捕获 urllib2.URLError try: ...

  2. Bootstrap 实现CRUD示例及代码

    https://github.com/wenzhixin/bootstrap-table-examples/blob/master/crud/index.html <!DOCTYPE html& ...

  3. php 通过类名获取类的文件地址

    $reflector = new ReflectionClass("Child"); $fn = $reflector->getFileName(); return dirn ...

  4. CXF浅析

    CXF 框架支撑环境    CXF 框架是一种基于 Servlet 技术的 SOA 应用开发框架,要正常运行基于 CXF 应用框架开发的企业应用,除了 CXF 框架本身之外,还需要 JDK 和 Ser ...

  5. [BZOJ3456]城市规划(生成函数+多项式求逆+多项式求ln)

    城市规划 时间限制:40s      空间限制:256MB 题目描述 刚刚解决完电力网络的问题, 阿狸又被领导的任务给难住了.  刚才说过, 阿狸的国家有n个城市, 现在国家需要在某些城市对之间建立一 ...

  6. hdu 5692 Snacks(dfs时间戳+线段树)

    Snacks Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Sub ...

  7. Android手机 "已安装了存在签名冲突的同名数据包"

    如果你不是开发者:如果你在android上更新一个已经安装过较早版本软件时,安装到最后一步提示你:已安装了存在签名冲突的同名数据包,然后安装失败.这是因为旧版软件的签名信息与新版不一致造成的.你可以卸 ...

  8. bzoj 1650: [Usaco2006 Dec]River Hopscotch 跳石子

    1650: [Usaco2006 Dec]River Hopscotch 跳石子 Time Limit: 5 Sec  Memory Limit: 64 MB Description Every ye ...

  9. mysql备份相关

    1linux下mysql导出文件 备份mysql数据库的命令 mysqldump -h主机名 -u用户名 -p密码 数据库名字 > 备份的数据库名字.sql 例如:mysqldump  -uro ...

  10. MYSQL复习笔记10-连接

    Date: 20140219Auth: Jin 一.介绍连接是二元运算,可以对两个表或多个表进行查询.T-SQL中分两大类,一是符合SQL标准的连接谓词表示形式,二是T-SQL扩展的使用关键字JOIN ...