28
假设我知道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
}
}
}
所以,对于一个Type t
,目的是调用FooMethod()
上是Type t
类。基本上我需要扭转typeof()
运营商。
感谢伊戈尔,这将工作正常(虽然我对C#感到失望 - 它看起来完全没有类型安全) 在我的实际代码中有很多类可能在Type变量中,所以反射是必需的。 – MrEff 2010-08-02 16:24:35