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 许多现代编程语言 ...
随机推荐
- 转:windbg调试堆
转:http://www.cnblogs.com/dsky/archive/2013/05/15/3079363.html 简评: 代码中采用malloc/free进行堆申请,实际调用的仍然是Heap ...
- [hdu-3007]Buried memory 最小覆盖圆
大致题意: 平面上有n个点,求一个最小的圆覆盖住所有点 最小覆盖圆裸题 学习了一波最小覆盖圆算法 #include<cstdio> #include<iostream> #in ...
- 洛谷——P2681 众数
P2681 众数 题目背景 Alice和Bob玩游戏 题目描述 Alice现在有一个序列a1.a2...an 现在她需要Bob支持询问一个区间内的众数,还要支持修改一个位置的ai 输入输出格式 输入格 ...
- MYSQL注入天书之导入导出介绍
Background-3 导入导出相关操作的讲解 load_file()导出文件 Load_file(file_name):读取文件并返回该文件的内容作为一个字符串. 使用条件: A.必须有权限读取并 ...
- poj 2262 筛法求素数(巧妙利用数组下标!)
Goldbach's Conjecture Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 41582 Accepted: ...
- RUP你知道多少?
RUP 相信学UML的同学,对此都很耳熟,当然也眼熟,可是,对于RUP,你了解多少呢? 首先,什么是RUP? RUP是Rational UnifiedProcess,统一软件开发过程,是一个面向对象且 ...
- Line Reflection -- LeetCode
Given n points on a 2D plane, find if there is such a line parallel to y-axis that reflect the given ...
- luogu P1440 求m区间内的最小值
题目描述 一个含有n项的数列(n<=2000000),求出每一项前的m个数到它这个区间内的最小值.若前面的数不足m项则从第1个数开始,若前面没有数则输出0. 输入输出格式 输入格式: 第一行两个 ...
- [BZOJ1975][SDOI2010]魔法猪学院(k短路,A*)
1975: [Sdoi2010]魔法猪学院 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 2748 Solved: 883[Submit][Statu ...
- 【欧拉函数】BZOJ2818-GCD
怎么漏了这一道……本来想要水一水,结果忘记了φ[1]=1,果然要滚一遍前面的知识…… #include<iostream> #include<cstdio> #include& ...