zoukankan      html  css  js  c++  java
  • C# Meta Programming

    <#@ 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;
      }
    }
  • 相关阅读:
    Java Executors小结
    Java取得一个对象里所有get方法和set方法, 读取某个类下所有变量的名称
    js中的this
    style,ng-style, ng-attr-style的对比
    keil 赋值之后再声明变量提示错误error: #268: declaration may not appear after executable statement in block
    网络字节顺序为大端模式
    MDK警告 warning: #111-D: statement is unreachable
    #231-D: declaration is not visible outside of function
    linux修改文件所有者和文件所在组
    getpwuid()
  • 原文地址:https://www.cnblogs.com/davidgu/p/3998591.html
Copyright © 2011-2022 走看看