Skip to content

Using reflection to invoke a method

George George

Asked Sep 7, 2008, 6:19 PM EDT2 answers
Share
LinkedInX

Hello everyone,


There are two ways to use reflection to invoke a method,

1. Using InvokeMember;
2. Using GetType.GetMethod to retrieve a MethodInfo object, then use the MethodInfo object to invoke the method.

My point is from the result point of view, they are the same, but method 2 if more efficient if we want to invoke the method multiple of times by the same MethodInfo object -- since each time we call InvokeMember, it needs to find the method, nothing saved as MethodInfo object in method 2 and each time InvokeMember did the binding to find the same method.

My question is whether my point of understanding is correct and whether there any more differences/pros/cons compared with the 2 methods.


thanks in advance,
George

2 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • Bechir Bejaoui
    +1
    I suggest an operational solution to precise whether method1 or method2 is the more important

    create a class and within it impelment the two method 1 and 2 then foreach method do impelement a counter this counts the execution duration to make the measure
    +1
  • +1

    My question is not asking which one is faster, but why faster? Any comments? :-)

     

    regards,

    George

    +1