Click here to Skip to main content
15,922,309 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
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
Posted

Sounds reasonable to me. I never use InvokeMember.
 
Share this answer
 
George_George wrote:
My question is whether my point of understanding is correct and whether there any more differences/pros/cons compared with the 2 methods.


Looks like your understanding are correct.

If you know the method signature at compile time, you can gain more performance by creating a delegate from the MethodInfo using Delegate.CreateDelegate(Type, MethodInfo)[^] and invoking that delegate.

 
Share this answer
 


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900