C# Meta Programming - Let Your Code Generate Code - Introduction of The Text Template Transformation Toolkit(T4)-阿里云开发者社区

开发者社区> 开发与运维> 正文
登录阅读全文

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

 

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

分享:
开发与运维
使用钉钉扫一扫加入圈子
+ 订阅

集结各类场景实战经验,助你开发运维畅行无忧

其他文章
最新文章
相关文章