Fred
Fred

Reputation: 12826

MethodInfo.Invoke vs Type.InvokeMember?

What is the difference between the MethodInfo.Invoke() and Type.InvokeMember() methods?

Which should be used in which scenarios?

Which is the preferred way to invoke a method?

Upvotes: 4

Views: 1256

Answers (2)

MichaelDotKnox
MichaelDotKnox

Reputation: 1310

Depending upon how you use it, MethodInfo.Invoke could be faster. When you call Type.InvokeMember under the covers a MethodInfo is retrieved and then invoked. Retrieving a MethodInfo is expensive. If you are calling the same method multiple times, saving the MethodInfo in a variable and invoking it from the variable with save time.

Upvotes: 0

Helen Araya
Helen Araya

Reputation: 1946

I guess both should be the same as far as invoking a Method or constructor is concerted but InvokeMember must be used if you want to get/set a field/property.

Check the links here for more details.

see MethodBase.Invoke Example

see Type.InvokeMember Example

Upvotes: 0

Related Questions