我總是陷入了一個反思問題,我認爲這不是很大,但我沒有找到任何解決方案。C#反思這個對象
public class myClass : myClassIF {
public myClass() { }
private void doSomething_A() {
//...
}
private void doSomething_B() {
//...
}
public void DecideAndCall(string identifier) {
string methodName = "doSomething_" + identifier;
MethodInfo mi = this.GetType().GetMethod(methodName); //here i got a NullReference??
//here should be the Invocation of the Method and so on...
}
}
界面看起來是這樣的:
public interface myClassIF {
void DecideAndCall(string identifier);
}
如果我叫實現getMethod( 「...」) - 的方法,我總是得到一個NullReference。我無法理解這一點,因爲在這個項目的其他部分中,我之前完成了這個任務。但是在那裏,我使用了Refelction而不是「this」。
是否有可能在實際的實例化對象中反映方法?我想我應該是,但我不知道如何...
很多謝謝! Benni
你是如何調用`DecideAndCall`進行了討論?你確定傳遞「A」還是「B」?如果找不到方法,GetMethod將拋出ArgumentNullException,這可能就是你所看到的嗎?或者,將該語句分解爲幾個步驟,以確定哪個部分正在拋出Null引用。 – Lazarus 2011-01-28 12:37:36