Created
August 26, 2013 21:47
-
-
Save ws6/6347071 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 <Type> findsubclass( Type type) | |
{ | |
List<Type> types= new List<Type>(); | |
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<object> | |
initObjects( ref Dictionary<string, object> param, List <Type> types ) | |
{ | |
var ret=new List<object>(); | |
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<string, object> param) | |
{ | |
List <Type> types = findsubclass(basetype); | |
var objs = initObjects(ref param, types); | |
bool allGood = true; | |
foreach(var obj in objs) | |
{ | |
if( !callMethods(obj)) | |
allGood=false; | |
} | |
return allGood; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment