You write the following code to implement the BraindumpsClass.MyMethod function.
public class BraindumpsClass {
public int MyMethod(int arg) {
return arg;
}}
You need to call the BraindumpsClass.MyMethod function dynamically from an unrelated class in
your assembly. Which code segment should you use?
A. BraindumpsClass myClass = new BraindumpsClass();
Type t = typeof(BraindumpsClass);
MethodInfo m = t.GetMethod(“MyMethod”);
int i = (int)m.Invoke(this, new object[] { 1 });
B. BraindumpsClass myClass = new BraindumpsClass();
Type t = typeof(BraindumpsClass);
MethodInfo m = t.GetMethod(“MyMethod”);
int i = (int) m.Invoke(myClass, new object[] { 1 });
C. BraindumpsClass myClass = new BraindumpsClass();
Type t = typeof(BraindumpsClass);
MethodInfo m = t.GetMethod(“BraindumpsClass.MyMethod”);
int i = (int)m.Invoke(myClass, new object[] { 1 });
D. Type t = Type.GetType(“BraindumpsClass”);
MethodInfo m = t.GetMethod(“MyMethod”);
int i = (int)m.Invoke(this, new object[] { 1 });
Answer: B
No comments:
Post a Comment