C# Meta Programming - Let Your Code Generate Code - Introduction of The Text Template Transformation Toolkit(T4)

简介: using System; public static class GreaterTest { public static of( left, right) { return left.
<#@ 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) < 0 ? 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) < 0 ? right : left;
  }
  public static Byte of(Byte left, Byte right)
  {
    return left.CompareTo(right) < 0 ? right : left;
  }
  public static Char of(Char left, Char right)
  {
    return left.CompareTo(right) < 0 ? right : left;
  }
  public static Decimal of(Decimal left, Decimal right)
  {
    return left.CompareTo(right) < 0 ? right : left;
  }
  public static Double of(Double left, Double right)
  {
    return left.CompareTo(right) < 0 ? right : left;
  }
  public static Single of(Single left, Single right)
  {
    return left.CompareTo(right) < 0 ? right : left;
  }
  public static Int32 of(Int32 left, Int32 right)
  {
    return left.CompareTo(right) < 0 ? right : left;
  }
  public static Int64 of(Int64 left, Int64 right)
  {
    return left.CompareTo(right) < 0 ? right : left;
  }
  public static SByte of(SByte left, SByte right)
  {
    return left.CompareTo(right) < 0 ? right : left;
  }
  public static Int16 of(Int16 left, Int16 right)
  {
    return left.CompareTo(right) < 0 ? right : left;
  }
  public static String of(String left, String right)
  {
    return left.CompareTo(right) < 0 ? right : left;
  }
  public static UInt32 of(UInt32 left, UInt32 right)
  {
    return left.CompareTo(right) < 0 ? right : left;
  }
  public static UInt64 of(UInt64 left, UInt64 right)
  {
    return left.CompareTo(right) < 0 ? right : left;
  }
  public static UInt16 of(UInt16 left, UInt16 right)
  {
    return left.CompareTo(right) < 0 ? right : left;
  }
}

 

目录
相关文章
|
2月前
|
SQL 开发框架 .NET
C# Linq SaveChanges()报错 You have an error in your SQL syntex
C# Linq SaveChanges()报错 You have an error in your SQL syntex
10 0
|
算法 C# Java
使用C# (.NET Core) 实现模板方法模式 (Template Method Pattern)
本文的概念内容来自深入浅出设计模式一书. 项目需求 有一家咖啡店, 供应咖啡和茶, 它们的工序如下: 咖啡: 茶: 可以看到咖啡和茶的制作工序是差不多的, 都是有4步, 其中有两步它们两个是一样的, 另外两步虽然具体内容不一样, 但是都做做的同一类工作.
1309 0
|
C# 数据格式 XML
C#,VB.NET如何将Word转换为PDF和Text
众所周知,Word是我们日常工作中常用的办公软件之一,有时出于某种需求我们需要将Word文档转换为PDF以及Text。那么如何以C#,VB.NET编程的方式来实现这一功能呢? 下面我将分开介绍如何运用免费版的Spire.Doc for .NET组件来实现Word到PDF以及Text的转换。
1666 0
|
C# 数据格式 XML
C#,VB.NET 如何将Excel转换为Text
在工作中,有时我们需要转换文档的格式,之前已经跟大家介绍过了如何将Excel转换为PDF。今天将与大家分享如何将Excel转换为Text。这次我使用的依然是免费版的Spire.XLS for .NET组件。
1217 0
|
程序员 C# 测试技术
C# Meta Programming - Let Your Code Generate Code - 利用反射重写自动的ToString()
我们在写一些Model的时候,经常会重写ToString,为了在控制台中进行打印或者更好的单元测试。 但是,如果Model的字段非常多的时候,如此简单的重复劳动经常会变成一件令人头痛的事情,因为大家 都不想重复劳动,或者这种事情应该交给初级程序员或者毕业生去做。
880 0