tsunami

log in
history

C#: Examine method overloads

Luke Breuer
2008-03-17 20:14 UTC

C# 3.0
using System;
using System.Linq;
using System.Reflection;
using System.Collections.Generic;

class Program
{
    static void Main(string[] args)
    {
        Func<MethodInfo, bool> selectThis = mi => mi.Name == "CreateDomain"; 
        Func<ParameterInfo, string> paramToString = pi => 
            string.Format("  {0,-20} : {1}", pi.ParameterType.Name, pi.Name);
        Func<MethodInfo, string> methodToString = mi =>
            mi.ReturnType.Name + " : " + mi.Name + "\n" + 
                mi.GetParameters().Select(paramToString).Join("\n");

        Console.WriteLine(typeof(AppDomain)
            .GetMethods()
            .Where(selectThis)
            .Select(methodToString)
            .Join("\n\n\n"));
    }
}

static class Functional
{
    public static string Join(this IEnumerable<string> strings, string delimiter)
    {
        return strings.Any()
            ? string.Join(delimiter, strings as string[] ?? strings.ToArray())
            : "";
    }
}
C# 2.0
using System;
using System.Reflection;

class Program
{
    static void Main(string[] args)
    {
        Predicate<MethodInfo> selectThis = delegate(MethodInfo mi) 
        { 
            return mi.Name == "CreateDomain"; 
        };
        Converter<ParameterInfo, string> paramToString = delegate(ParameterInfo pi)
        {
            return string.Format("  {0,-20} : {1}", pi.ParameterType.Name, pi.Name);
        };
        Converter<MethodInfo, string> methodToString = delegate(MethodInfo mi)
        {
            return mi.ReturnType.Name + " : " + mi.Name + "\n" + 
                string.Join("\n", Array.ConvertAll(mi.GetParameters(), paramToString));
        };

        MethodInfo[] methods = typeof(AppDomain).GetMethods();
        MethodInfo[] methodsWeCareAbout = Array.FindAll(methods, selectThis);

        Console.WriteLine(string.Join("\n\n\n", Array.ConvertAll(methodsWeCareAbout, methodToString)));
    }
}
WinXP, targetting .NET 2.0
AppDomain : CreateDomain
  String               : friendlyName
  Evidence             : securityInfo
  AppDomainSetup       : info


AppDomain : CreateDomain
  String               : friendlyName
  Evidence             : securityInfo


AppDomain : CreateDomain
  String               : friendlyName
  Evidence             : securityInfo
  String               : appBasePath
  String               : appRelativeSearchPath
  Boolean              : shadowCopyFiles


AppDomain : CreateDomain
  String               : friendlyName


AppDomain : CreateDomain
  String               : friendlyName
  Evidence             : securityInfo
  AppDomainSetup       : info
  PermissionSet        : grantSet
  StrongName[]         : fullTrustAssemblies


AppDomain : CreateDomain
  String               : friendlyName
  Evidence             : securityInfo
  String               : appBasePath
  String               : appRelativeSearchPath
  Boolean              : shadowCopyFiles
  AppDomainInitializer : adInit
  String[]             : adInitArgs
Nemerle
#pragma indent

using System
using System.Console
using System.Reflection
using Nemerle.IO

def methods = $[(m.Name, m.ReturnType.Name,
                 $[(p.ParameterType.Name, p.Name) | p in m.GetParameters()])
                | m in typeof(AppDomain).GetMethods(),
                  m.Name == "CreateDomain" ]

foreach ((name, ret, args) in methods)
  print("$ret : $name\n")
  foreach ((typename, name) in args)
    WriteLine(string.Format("  {0,-20} : {1}", typename, name))
  print("\n\n")
Scala
package uk.co.zootm.scalatest
 
import java.lang.reflect.Method
import javax.swing.JComboBox
 
object Program extends Application {
    println( describeMethod( (new JComboBox).getClass, "contains" ) )
 
    def describeMethod( clazz: Class, methodName: String ): String = 
        clazz.getMethods.
            filter( _.getName == methodName ).
            map( describeMethod( _ ) ).
            mkString( "\n\n\n" )
 
    def describeMethod( method: Method ) = 
            method.getName + 
            method.getParameterTypes.
                map( "\n    " + _.getSimpleName ).
                mkString
}