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


in code have functions call type.getmethod(string, type[]) on various static functions of clr. maybe 30 or different static functions being looked in total. being looked repeatedly , because parent functions being called often. assume clr cache methodinfo results first call each unique looked-up method costly, confirmation.

edit: methodinfo results end being arguments expression.call() statements in expression trees.

would better/more dependable implement own cache? if did so, can cache methodinfo results once @ start of app, , reaccess them indefinitely? or should use runtimemethodhandle instead this:

// obtaining handle memberinfo runtimemethodhandle handle = typeof(d).getmethod("mymethod").methodhandle; // resolving handle memberinfo methodbase mb = methodinfo.getmethodfromhandle(handle); 

that snippet comes from: https://msdn.microsoft.com/en-us/magazine/cc163759.aspx#s8

it's not clear me why cache runtimemethodhandle instead of methodinfo.

my problem not speedy invocation making sure type.getmethod() calls not incurring big hits every time being called on same methods

then need cache methodinfo object given method name. clr not guarantee cache you, , indeed can expensive call getmethod(), no matter how it, depending on whether data still cached or not. reflection in general expensive. should eschewed if @ possible, when performance concern.

in case, since dealing expression objects, depending on how you're using them (without a good, minimal, complete code example it's not possible know sure what's best in scenario), may find makes more sense cache compiled expressions. more generally, if going cache @ all, makes sense cache result farthest down processing invariant cache key.

note clr does cache runtime member resolution dynamic type objects. if didn't want mess implementation of cache yourself, it's possible incorporate dynamic expressions , let clr handle caching of dynamically-named methods. again, without more details of specific scenario, it's not possible sure whether work, never mind whether it's useful approach in case.


Comments