Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Reflection;
- namespace BlabberServer
- {
- public static class Reflector
- {
- public static List<KeyValuePair<T1, T2>> FindAllMethods<T1, T2>()
- where T1 : Attribute
- where T2 : class
- {
- if(!typeof(T2).IsSubclassOf(typeof(Delegate))) return null;
- List<KeyValuePair<T1, T2>> results = new List<KeyValuePair<T1, T2>>();
- foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
- {
- if (assembly.GlobalAssemblyCache) continue;
- Type[] types = assembly.GetTypes();
- foreach(Type type in types)
- {
- MethodInfo[] methods = type.GetMethods();
- foreach(MethodInfo method in methods)
- {
- T1 attribute = Attribute.GetCustomAttribute(method, typeof(T1), false) as T1;
- if (attribute == null) continue;
- T2 callback = Delegate.CreateDelegate(typeof(T2), method, false) as T2;
- if (callback == null) continue;
- results.Add(new KeyValuePair<T1, T2>(attribute, callback));
- }
- }
- }
- return results;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment