mardi 2 juin 2015

C# 4.5 - Are repeated calls to Type.GetMethod() cached internally?

In my code I have some functions that call Type.GetMethod(string, Type[]) on some various static functions of the CLR. Maybe 30 or so different static functions are being looked up in total. But they are being looked up repeatedly and often because the parent functions are being called often. I assume the CLR would cache the MethodInfo results so only the first call for each unique looked-up method is costly, but I would like some confirmation.

Would it be better/more dependable to implement my own cache? If I did so, can I cache the MethodInfo results once at the very start of the app, and just reaccess them indefinitely? Or should I use RuntimeMethodHandle instead like this:

// Obtaining a Handle from an MemberInfo
RuntimeMethodHandle handle = typeof(D).GetMethod("MyMethod").MethodHandle;
// Resolving the Handle back to the MemberInfo
MethodBase mb = MethodInfo.GetMethodFromHandle(handle);

That snippet comes from: http://ift.tt/1eOkDek

It's not clear to me why I would cache a RuntimeMethodHandle instead of just the MethodInfo.





Aucun commentaire:

Enregistrer un commentaire