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;
  }
}

 

目录
相关文章
|
C# 开发者
C# 一分钟浅谈:Code Contracts 与契约编程
【10月更文挑战第26天】本文介绍了 C# 中的 Code Contracts,这是一个强大的工具,用于通过契约编程增强代码的健壮性和可维护性。文章从基本概念入手,详细讲解了前置条件、后置条件和对象不变量的使用方法,并通过具体代码示例进行了说明。同时,文章还探讨了常见的问题和易错点,如忘记启用静态检查、过度依赖契约和性能影响,并提供了相应的解决建议。希望读者能通过本文更好地理解和应用 Code Contracts。
268 3
|
11月前
|
C# 开发工具 C++
code runner 运行C#项目
本文介绍了如何修改Code Runner设置使 Visual Studio Code (VS Code) 能直接运行完整的 C# 项目。传统方式依赖 cscript 工具,仅支持 .csx 文件,功能受限且已停止维护。新配置使用 `dotnet run` 命令,结合一系列炫酷的cmd指令,将指令定位到具体的csproj文件上进行运行。
580 38
|
存储 API C#
【Azure Developer】解决Azure Key Vault管理Storage的示例代码在中国区Azure遇见的各种认证/授权问题 - C# Example Code
【Azure Developer】解决Azure Key Vault管理Storage的示例代码在中国区Azure遇见的各种认证/授权问题 - C# Example Code
194 0
|
SQL 开发框架 .NET
C# Linq SaveChanges()报错 You have an error in your SQL syntex
C# Linq SaveChanges()报错 You have an error in your SQL syntex
86 0
|
消息中间件 Java API
阿里云微服务消息队列Token C# Code Sample
文本主要演示使用C# SDK调用 ApplyToken 创建临时访问 Token。
1198 0
阿里云微服务消息队列Token C# Code Sample
|
算法 C# Java
使用C# (.NET Core) 实现模板方法模式 (Template Method Pattern)
本文的概念内容来自深入浅出设计模式一书. 项目需求 有一家咖啡店, 供应咖啡和茶, 它们的工序如下: 咖啡: 茶: 可以看到咖啡和茶的制作工序是差不多的, 都是有4步, 其中有两步它们两个是一样的, 另外两步虽然具体内容不一样, 但是都做做的同一类工作.
1497 0