我有几个静态类在命名空间mySolution.Macros如

static class Indent{

public static void Run(){

// implementation

}

// other helper methods

}

所以我的问题是如何可以用反射的帮助调用这些方法?

如果方法,其中不是静态的,那么我可以做一些像:

var macroClasses = Assembly.GetExecutingAssembly().GetTypes().Where( x => x.Namespace.ToUpper().Contains("MACRO") );

foreach (var tempClass in macroClasses)

{

var curInsance = Activator.CreateInstance(tempClass);

// I know have an instance of a macro and will be able to run it

// using reflection I will be able to run the method as:

curInsance.GetType().GetMethod("Run").Invoke(curInsance, null);

}

我想保持我的类静态。我将如何使用静态方法做类似的事情?

总之,我想从名为mySolution.Macros的所有静态类中调用所有的Run方法。

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐