背景

我需要为int、long、float等这些数值类型写一些扩展方法,但是我发现他们不是一个继承体系,我的第一个思维就是需要为每个类型重复写一遍扩展方法,这让我觉得非常不爽,但是我还是不情愿的写了,等int和long写完后,我突然觉得我可以让T4帮我写,而且C#支持部分类,就更爽了。

用T4实现

模板(写代码的代码)

 <#@ template debug="false" hostspecific="false" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ output extension=".cs" #>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO; namespace Happy.Infrastructure.ExtentionMethods
{
public static partial class Check
{
<#
foreach(var type in new string[]{ "double", "int" })
{
#>
/// <summary>
/// 名称为<paramref name="variableName"/>的参数或变量的值(<paramref name="value"/>)必须在某个范围。
/// </summary>
public static void MustBetween(this <#= type #> value, string variableName, <#= type #> start, <#= type #> end)
{
Require(value >= start && value <= end, String.Format(Messages.Error_MustBetween, variableName, start, end));
} /// <summary>
/// 名称为<paramref name="variableName"/>的参数或变量的值(<paramref name="value"/>)必须大于指定的值。
/// </summary>
public static void MustGreaterThan(this <#= type #> value, string variableName, <#= type #> target)
{
Require(value > target, String.Format(Messages.Error_MustGreaterThan, variableName, target));
} /// <summary>
/// 名称为<paramref name="variableName"/>的参数或变量的值(<paramref name="value"/>)必须大于等于指定的值。
/// </summary>
public static void MustGreaterThanEqual(this <#= type #> value, string variableName, <#= type #> target)
{
Require(value > target, String.Format(Messages.Error_MustGreaterThanEqual, variableName, target));
} <#
}
#>
}
}

我对了吗?

当我为这种用法暗自高兴的时候,有位兄弟给了我更好的意见:

看到这段话的那一刻是我当天最开心的时刻,谢谢郭明锋兄弟。

我在反思为啥当时我只思考他们不是一个继承体系,而忽略了他们或许会都实现某个接口呢,意识+经验非常重要,相信这次是一个比较好的经历。

改进后的版本

         /// <summary>
/// 名称为<paramref name="variableName"/>的参数或变量的值(<paramref name="value"/>)必须在某个范围。
/// </summary>
public static void MustBetween<T>(this T value, string variableName, T start, T end)
where T : IComparable<T>
{
Require(value.CompareTo(start) >= && value.CompareTo(end) <= , String.Format(Messages.Error_MustBetween, variableName, start, end));
} /// <summary>
/// 名称为<paramref name="variableName"/>的参数或变量的值(<paramref name="value"/>)必须大于指定的值。
/// </summary>
public static void MustGreaterThan<T>(this T value, string variableName, T target)
where T : IComparable<T>
{
Require(value.CompareTo(target) > , String.Format(Messages.Error_MustGreaterThan, variableName, target));
} /// <summary>
/// 名称为<paramref name="variableName"/>的参数或变量的值(<paramref name="value"/>)必须大于等于指定的值。
/// </summary>
public static void MustGreaterThanEqual<T>(this T value, string variableName, T target)
where T : IComparable<T>
{
Require(value.CompareTo(target) >= , String.Format(Messages.Error_MustGreaterThanEqual, variableName, target));
}

备注

再次感谢(郭明锋)http://www.cnblogs.com/guomingfeng/

.NET:用T4消除代码重复,对了,也错了的更多相关文章

  1. 用T4消除代码重复,对了,也错了

    用T4消除代码重复,对了,也错了 背景 我需要为int.long.float等这些数值类型写一些扩展方法,但是我发现他们不是一个继承体系,我的第一个思维就是需要为每个类型重复写一遍扩展方法,这让我觉得 ...

  2. T4:使用 T4 消除程序和配置文件中重复的字符串信息

    背景 我们经常在配置文件中配置各种:id.name,然后在程序中使用这些配置获取信息,这导致了字符串重复出现在系统的多个地方,非常不利于维护,本文介绍采用 T4 来消除这种重复. T4 消除重复 配置 ...

  3. [转]从数据到代码——基于T4的代码生成方式

    本文转自:http://www.cnblogs.com/artech/archive/2010/10/23/1859529.html 在之前写一篇文章<从数据到代码>(上篇.下篇)中,我通 ...

  4. 代码重复检查工具——python的使用CPD比较好用,clone digger针对py2,其他有名的如Simian PMD-CPD CloneDR CCCD CCFinder Bauhaus CodePro

    代码重复检测: cpd --minimum-tokens 100 --files g:\source\python\ --language python >log.txt 输出类似: ===== ...

  5. java消除 list重复值及交集,并集,差集

    消除 list重复值 Java代码  public void removeDuplicate(List list) { HashSet h = new HashSet(list); list.clea ...

  6. Scala减少代码重复

    高阶函数可以把其它函数当作函数参数,帮助我们减少代码重复,例如: object FileMatcher { private def fileHere = (new File(".\\file ...

  7. 通过Groovy来消除代码噪声

    通过Groovy来消除代码噪声 Java是在JVM上运行的最广泛使用的编程语言.不过,还有很多其他基于JVM的语言,比如Groovy,Scala,JRuby,Jython,Kotlin等等.其中,Gr ...

  8. 在C#中动态编译T4模板代码

    转: http://www.wxzzz.com/1438.html 资料: https://cnsmartcodegenerator.codeplex.com/SourceControl/latest ...

  9. 使用Java8中的Optional类来消除代码中的null检查

    简介 Optional类是Java 8新增的一个类,Optional 类主要解决的问题是臭名昭著的空指针异常(NullPointerException). —— 每个 Java 程序员都非常了解的异常 ...

随机推荐

  1. 开始学习MaxCompute

    https://help.aliyun.com/document_detail/34615.html?spm=a2c4g.11186623.6.688.jVxTMW

  2. CentOS7.5下时间戳转换为时间

    Unix时间戳(英文为Unix epoch, Unix time, POSIXme 或 Unix timestamp)是从1970年1月1日(UTC/GMT的午夜)开始所经过的秒数,不考虑闰秒 一.查 ...

  3. python模拟QQ聊天室(tcp加多线程)

    python模拟QQ聊天室(tcp加多线程) 服务器代码: from socket import * from threading import * s = socket(AF_INET,SOCK_S ...

  4. phantomjs2.1 初体验

    上次看了一下scrapy1.1的新手指南 决定写个小爬虫实验一下 目标网站是http://www.dm5.com/manhua-huofengliaoyuan准备爬取漫画火凤燎原的已有章节,将图片保存 ...

  5. 【转】Serializers 序列化组件

    https://www.cnblogs.com/MayDayTime/p/9890582.html 为什么要用序列化组件 当我们做前后端分离的项目~~我们前后端交互一般都选择JSON数据格式,JSON ...

  6. lua常用函数

    select (index, ···) 功能:当index为数字将返回所有index大于index的参数: 如:select(2,"a","b","c ...

  7. 不通过注册表使用ActiveX对象

    为了弄清楚COM库的运行原理,特意做了这个实验: #include "stdafx.h" #include "objbase.h" #include " ...

  8. ref:【干货分享】PHP漏洞挖掘——进阶篇

    ref:http://blog.nsfocus.net/php-vulnerability-mining/ [干货分享]PHP漏洞挖掘——进阶篇 王陶然     从常见的PHP风险点告诉你如何进行PH ...

  9. CodeForces 909D Colorful Points

    题解: 暴力,模拟. 把字符串压缩一下,相同的处理成一位,记录下个数,然后暴力模拟即可. #include <bits/stdc++.h> using namespace std; con ...

  10. BNUOJ 52516 Just A String

    $KMP$. 枚举每一个后缀,去原串中进行匹配,每次匹配到原串到$i$位置的后缀与这次枚举的后缀的前缀,更新答案. #include<bits/stdc++.h> using namesp ...