Guest User

Untitled

a guest
May 14th, 2012
25
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.35 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Reflection;
  4.  
  5. namespace BlabberServer
  6. {
  7.     public static class Reflector
  8.     {
  9.         public static List<KeyValuePair<T1, T2>> FindAllMethods<T1, T2>()
  10.             where T1 : Attribute
  11.             where T2 : class
  12.         {
  13.             if(!typeof(T2).IsSubclassOf(typeof(Delegate))) return null;
  14.             List<KeyValuePair<T1, T2>> results = new List<KeyValuePair<T1, T2>>();
  15.             foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
  16.             {
  17.                 if (assembly.GlobalAssemblyCache) continue;
  18.                 Type[] types = assembly.GetTypes();
  19.                 foreach(Type type in types)
  20.                 {
  21.                     MethodInfo[] methods = type.GetMethods();
  22.                     foreach(MethodInfo method in methods)
  23.                     {
  24.                         T1 attribute = Attribute.GetCustomAttribute(method, typeof(T1), false) as T1;
  25.                         if (attribute == null) continue;
  26.                         T2 callback = Delegate.CreateDelegate(typeof(T2), method, false) as T2;
  27.                         if (callback == null) continue;
  28.                         results.Add(new KeyValuePair<T1, T2>(attribute, callback));
  29.                     }
  30.                 }
  31.             }
  32.             return results;
  33.         }
  34.     }
  35. }
Advertisement
Add Comment
Please, Sign In to add comment