<#@ 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. Linux操作命令(三)

    本次实验将介绍 Linux 命令中 more.less.head.tail 命令的用法. more less head tail 1.more ·more功能类似cat,cat命令是将整个文件的内容从 ...

  2. 洛谷P1414 又是毕业季 [数论]

    题目传送门 又是毕业季 题目背景 “叮铃铃铃”,随着高考最后一科结考铃声的敲响,三年青春时光顿时凝固于此刻.毕业的欣喜怎敌那离别的不舍,憧憬着未来仍毋忘逝去的歌.1000多个日夜的欢笑和泪水,全凝聚在 ...

  3. 洛谷——P1346 电车

    P1346 电车 题目描述 在一个神奇的小镇上有着一个特别的电车网络,它由一些路口和轨道组成,每个路口都连接着若干个轨道,每个轨道都通向一个路口(不排除有的观光轨道转一圈后返回路口的可能).在每个路口 ...

  4. Sqli-labs less 11

    Less-11 从这一关开始我们开始进入到post注入的世界了,什么是post呢?就是数据从客户端提交到服务器端,例如我们在登录过程中,输入用户名和密码,用户名和密码以表单的形式提交,提交到服务器后服 ...

  5. leetcode7 Rerver Integer

    题意:数字反转 思路:醉了,提交了好几次,难点不在于怎么反转,而是判断是否益处,原题中给的是int,4个字节,32位,开始不知道怎么判断.现在知道了是limits.h中的INT_MIN和INT_MAX ...

  6. mvc4 to mvc5 orEF5 to EF6 ,(升级EF6)

    把后台MVC4 自动生成的网站从EF5.0 升级为 EF6.1.3 (6.0以上) 报错 找不到方法:“System.Data.Objects.ObjectContext System.Data.En ...

  7. 最近升级mysql5.7出现下面问题,ORDER BY clause is not in GROUP BY..this is incompatible with sql_mode=only_full_group_by

    执行sql: SELECT * FROM `user_link` WHERE `group_id` IN ('78', '79') GROUP BY `link_id` 报错: SQLSTATE[42 ...

  8. Redux 源码自己写了一遍

    <!DOCTYPE HTML> <html> <head> <meta http-equiv="content-type" content ...

  9. 【BZOJ 1528】 1528: [POI2005]sam-Toy Cars (贪心+堆)

    1528: [POI2005]sam-Toy Cars Description Jasio 是一个三岁的小男孩,他最喜欢玩玩具了,他有n 个不同的玩具,它们都被放在了很高的架子上所以Jasio 拿不到 ...

  10. [BZOJ4824][CQOI2017]老C的键盘(树形DP)

    4824: [Cqoi2017]老C的键盘 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 193  Solved: 149[Submit][Statu ...