Which code segment should you use?

You write the following code to implement the CertkillerClass.MyMethod function.
public class CertkillerClass {
public int MyMethod(int arg) {
return arg;
}
}
You need to call the CertkillerClass.MyMethod function dynamically from an unrelated class in your assembly.
Which code segment should you use?

You write the following code to implement the CertkillerClass.MyMethod function.
public class CertkillerClass {
public int MyMethod(int arg) {
return arg;
}
}
You need to call the CertkillerClass.MyMethod function dynamically from an unrelated class in your assembly.
Which code segment should you use?

A.
CertKillerClass myClass = new CertKillerClass();
Type t = typeof(CertKillerClass);
MethodInfo m = t.GetMethod(“MyMethod”);
int i = (int)m.Invoke(this, new object[] {1});

B.
CertKillerClass myClass = new CertKillerClass();
Type t = typeof(CertKillerClass);
MethodInfo m = t.GetMethod(“MyMethod”);
int i = (int)m.Invoke(myClass, new object[] {1});

C.
CertKillerClass myClass = new CertKillerClass();
Type t = typeof(CertKillerClass);
MethodInfo m = t.GetMethod(“CertKillerClass.MyMethod”);
int i = (int)m.Invoke(myClass, new object[] {1});

D.
Type t = type.GetType(CertKillerClass);
MethodInfo m = t.GetMethod(“MyMethod”);
int i = (int)m.Invoke(this, new object[] {1});

Explanation:
Use reflection to get MethodInfo object that corresponds to the MyMethod member function.
Call the Invoke() method of MethodInfo



Leave a Reply 1

Your email address will not be published. Required fields are marked *


networkmanagers

networkmanagers

Correct answer is B