using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Reflection; using System.Text.RegularExpressions; namespace checkhub.libhub { public delegate bool processResult(object result); public static class hub { public static List findsubclass( Type type) { List types= new List(); var asm = Assembly.GetAssembly(type); foreach (var t in asm.GetTypes()){ if (t.IsSubclassOf(type)) { types.Add(t); //Console.WriteLine("{0}", t.Name); } } return types; } public static MethodInfo[] getMethods(Type type) { return type.GetMethods(); } public static List initObjects( ref Dictionary param, List types ) { var ret=new List(); foreach(var type in types) { var o=Activator.CreateInstance(type, param); ret.Add(o); } return ret; } //a callback function public static bool callMethods(object obj, string filter="^Jifeng",processResult saverst=null ,bool debug=true ) { Type t = obj.GetType(); bool allsucceed = true; foreach(var method in getMethods(t)) { Match match = Regex.Match(method.Name, filter); if (match.Success) { var result = t.InvokeMember(method.Name, BindingFlags.Public | BindingFlags.Instance | BindingFlags.InvokeMethod, null, obj, null); if (saverst!=null && ! saverst(result)) allsucceed = false; } } return allsucceed; } public static bool runAll(Type basetype, ref Dictionary param) { List types = findsubclass(basetype); var objs = initObjects(ref param, types); bool allGood = true; foreach(var obj in objs) { if( !callMethods(obj)) allGood=false; } return allGood; } } }