<[EMAIL PROTECTED]> writes:
>
> Delegates are stored in assemble as Types and can be
> retrieved by calling Module.GetTypes().
> It can be distinguished from other types because it is subclass
> of System.Delegate.
>
> But how to obtain information about the shape of the delegate:
> what is return type and what are parameters types?
>

Hi,

hopefully the attached code snippet will help answer your Q.

--sigbjorn
http://galois.com/~sof/hugs98.net/

//
// throw-away delegate dumper  -- sof 4/02.
//
using System;
using System.Reflection;

public class App {

 public static MethodInfo GetDelegateMemberInfo (System.Type t,
                                                 System.String delName) {
   /* With the help of ildasm (and Richter Ch. 17), a C# 'delegate'
    * decl is compiled into a nested class containing an Invoke()
    * method (amongst other things.)
    */
   Type[] ns = t.GetNestedTypes();
   Type delTy = Type.GetType("System.Delegate");

   foreach (Type n in ns) {
     if (n.IsSubclassOf(delTy) && n.Name == delName) {
       try {
         return n.GetMethod("Invoke");
       } catch {
         continue;
       }
     }
   }
   return null;
 }

 // example delegate
 public delegate void Foo(int x, int y, bool z);

 public static void Main() {
   MethodInfo m = GetDelegateMemberInfo(Type.GetType("App"), "Foo");

   if (m != null) {
     Console.Write("{0} Foo(", m.ReturnType);
     ParameterInfo[] ps = m.GetParameters();
     int idx = 0;

     while (idx < ps.Length) {
       Console.Write("{0}", ps[idx].ParameterType);
       Console.Write(((idx+1) < ps.Length) ? "," : ")");
       idx++;
     }
     Console.WriteLine("");
   }
 }
};

Reply via email to