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

來源:互聯網
上載者:User

標籤:style   blog   color   io   ar   for   sp   div   on   

<#@ 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# Meta Programming - Let Your Code Generate Code - Introduction of The Text Template Transformation Toolkit(T4)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.