小编MrE*_*Eff的帖子

使用Type调用静态方法

如何从a调用静态方法Type,假设我知道Type变量的值和静态方法的名称?

public class FooClass {
    public static FooMethod() {
        //do something
    }
}

public class BarClass {
    public void BarMethod(Type t) {
        FooClass.FooMethod()          //works fine
        if (t is FooClass) {
            t.FooMethod();            //should call FooClass.FooMethod(); compile error
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

因此,给定一个Type t,目标是调用FooMethod()该类Type t.基本上我需要反转typeof()操作员.

c#

38
推荐指数
2
解决办法
4万
查看次数

标签 统计

c# ×1