C# Meta Programming - Let Your Code Generate Code - Introduction of The Text Template Transformation Toolkit(T4)
<#@ 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)的更多相关文章
- C# Meta Programming - Let Your Code Generate Code - 利用反射重写自动的ToString()
我们在写一些Model的时候,经常会重写ToString,为了在控制台中进行打印或者更好的单元测试. 但是,如果Model的字段非常多的时候,如此简单的重复劳动经常会变成一件令人头痛的事情,因为大家 ...
- 问题:Custom tool error: Failed to generate code for the service reference 'AppVot;结果:添加Service Reference, 无法为服务生成代码错误的解决办法
添加Service Reference, 无法为服务生成代码错误的解决办法 我的解决方案是Silverlight+WCF的应用,Done Cretiria定义了需要在做完Service端的代码后首先运 ...
- Python 与 meta programming
meta programming: 编写能改变语言语法特性或者运行时特性的程序 Meta- 这个前缀在希腊语中的本意是「在…后,越过…的」,类似于拉丁语的 post-,比如 metaphysics 就 ...
- 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 ...
- AFNetworking 遇到错误 Code=-1016 "Request failed: unacceptable content-type: text/plain"
在开发过程使用了AFNetworking库,版本2.x,先运行第一个官方例子(替换GET 后面的url即可): AFHTTPRequestOperationManager *manager = [AF ...
- Fortify漏洞之Dynamic Code Evaluation: Code Injection(动态脚本注入)和 Password Management: Hardcoded Password(密码硬编码)
继续对Fortify的漏洞进行总结,本篇主要针对 Dynamic Code Evaluation: Code Injection(动态脚本注入) 和 Password Management: Har ...
- 解决 VS Code「Code Runner」插件运行 python 时的中文乱码问题
描述 这里整理了两种 VS Code「Code Runner」插件运行 python 时乱码的解决方案.至于设置「Auto Guess Encoding」为 true 的操作这里就不多描述了. 乱码截 ...
- 在vue中获取微信支付code及code被占用问题的解决?
这个地方坑比较多,查看网上并没有详细的文档,新手一般写到这里很痛苦.这里我只介绍一下我解决的方案,虽然它不是最好的,但是可行的方案: 总体分两步 1)跳到微信支付链接,它会自动拼接上code 2)获取 ...
- Fortify Audit Workbench 笔记 Dynamic Code Evaluation: Code Injection
Dynamic Code Evaluation: Code Injection Abstract 在运行时中解析用户控制的指令,会让攻击者有机会执行恶意代码. Explanation 许多现代编程语言 ...
随机推荐
- 再聊语言,模式,OOD
今天与人再次聊到这个话题,有人在为"到底该用什么模式"而烦恼,我相信,每个都经历过这个阶段一定都会感觉很熟悉这个烦恼我认为, 模式不是目的,只是工具,达到设计目标的工具,我们不会因 ...
- php定位并且获取天气信息
/** *获取天气预报信息 **/ header("Content-type: text/html; charset=utf-8"); class getWeather{ priv ...
- socket编程(TCP)
1.模型:(图片来自百度百科) 服务端: 1)创建socket对象 int socket(int domain, int type, int protocol) domain:即协议域,又称为协议族( ...
- 单例(LintCode)
单例 单例 是最为最常见的设计模式之一.对于任何时刻,如果某个类只存在且最多存在一个具体的实例,那么我们称这种设计模式为单例.例如,对于 class Mouse (不是动物的mouse哦),我们应将其 ...
- luogu P1047 校门外的树
题目描述 某校大门外长度为L的马路上有一排树,每两棵相邻的树之间的间隔都是1米.我们可以把马路看成一个数轴,马路的一端在数轴0的位置,另一端在L的位置:数轴上的每个整数点,即0,1,2,……,L,都种 ...
- 用js给循环的列表添加click事件
纠结了两天终于搞定了,首先id这个东西必不可少,这个时候不能用onclik事件,而是需要使用代理事件. 比如说,这里有个列表如下: <ul> <li></li> & ...
- iOS 11 安全区域适配
//解决iOS11,仅实现heightForHeaderInSection,没有实现viewForHeaderInSection方法时,section间距大的问题 [UITableView appea ...
- Web API使用记录系列(一)创建API项目与基本配置
本系列文章主要记录Web API使用过程中的一些个人总结,包括创建API项目.基础配置.ApiTestClient使用与HelpPage页面的优化.Owin与OAuth的使用等. 本节主要内容是API ...
- win7与Ubuntu 13.04双系统修改启动项顺序
在在win7下安装了Ubuntu 13.04后,在grub中,win7启动想是最后一个,为了把win7设置为默认启动项,需要更改grub设置.google了半天,发现更改/etc /default/g ...
- linux图机界面机制
1.X WindowX Window 是由麻省理工学院(MIT)推出的窗口系统,简称X,它旨在建立不依赖于特定硬件系统的图形和文字显示窗口系统的标准.1987 年9 月,MIT 推出了X 系统的11 ...