android反射 静态方法调用,c# – 使用反射调用静态方法

博客围绕如何使用反射调用静态类的方法展开。作者有几个静态类在命名空间mySolution.Macros中,想调用其中所有静态类的Run方法。作者给出了非静态方法的调用示例,最后强调想从该命名空间的所有静态类中调用Run方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我有几个静态类在命名空间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方法。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值