<#@ 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创建XML(转)

    在官方网站没有找到多少有用的知识.结果在Python and XML: An Introduction找到了一篇教程,抽空对照做,然后再总结分享出来.先来一个简单的: from xml.dom.min ...

  2. POJ 3171.Cleaning Shifts-区间覆盖最小花费-dp+线段树优化(单点更新、区间查询最值)

    Cleaning Shifts Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4721   Accepted: 1593 D ...

  3. sqlmap os shell解析

    0x00 Background 最近遇到测试环境,最后利用sqlmap的--os-shell参数取得shell.一直以来,对这个参数的工作原理不是十分的清晰.大致的思想应该是将脚本插入到数据库中,然后 ...

  4. Flask实战第37天:服务器权限验证

    完成服务器权限验证之前,我们先如下页面先补上 帖子管理 {% extends 'cms/cms_base.html' %} {% block title %} 帖子管理-CMS管理系统 {% endb ...

  5. sqlldr load UTF8 error

    The default length semantics for all datafiles (except UFT-16) is byte. So in your case you have a C ...

  6. Polynomial Problem(hdu 1296 表达式求值)

    We have learned how to obtain the value of a polynomial when we were a middle school student. If f(x ...

  7. 【Miller-Rabin算法】

    存个板子,应该是对的吧……没太试 http://www.cnblogs.com/Norlan/p/5350243.html Matrix67写的 根据wiki,取前9个素数当base的时候,long ...

  8. Vue视图下

    3 Vue视图 3.5 样式绑定 class绑定 <p :class='对象'> <p :class="数组"> <p :class="{类 ...

  9. Java反射机制的简单学习

    今天看了一下Java的反射机制,就此记录一下. 首先,我们要先了解一下什么是反射? 反射的概念是由Smith在1982年首次提出的,主要是指程序可以访问.检测和修改它本身状态或行为的一种能力. JAV ...

  10. Ubuntu 16.04修改MAC地址以及网络常用设置(IP/DNS/网关)

    1.先停止桌面版自带的NetworkManager,这东西很难用,且有些设置需要重启. sudo systemctl stop NetworkManager.service sudo systemct ...