Is it safe to cache the results of a parameterless method using the MethodHandle method as a key?

The WCF client application there are a number bezparametricheskih methods for which we want to cache the results - GetAllFoo(), GetAllBar(). They are used to fill out drop-down lists, etc., and the results do not change during the life of the client.

These results are currently cached by a unique string stored in the resource file — for example, GetAllCountries()cached with a resource CountryKey. The service is called only when the cache does not contain the requested key.

public T Get<T, V>(string key, Func<V, T> serviceCall, V proxy)
{
    if (!cache.Contains(key))
    {
        cache.Add(key, serviceCall(proxy));
    }   
    return cache.GetData(key) as T;
}

This is fine, except that we must maintain the keys in the resource file and be sure that each method uses the correct cache key, otherwise the situation will break. Old Control + C, Control + V causes a few headaches here, and I don't want them to check every place that this method calls.

So the question is:

The delegate serviceCallhas a property on it Methodthat describes how it is executed. This is an instance of MethodInfo, which in turn contains a property MethodHandle. Do I correctly believe that the property MethodHandleuniquely and consistently identifies the method referenced?

I would change the wrapper to

public T Get<T, V>(Func<V, T> serviceCall, V proxy)
{
    var key = serviceCall.Method.MethodHandle;
    // etc

which encapsulates caching and key issues correctly and removes any dependency on the calling "correct solution".

  • , MethodHandle -
  • , MethodHandle -
  • DO, MethodHandle - , , , ,
  • DO, MethodHandle - , ( ) .
+5
1

MSDN, MethodHandle, : " ".

http://msdn.microsoft.com/en-us/library/system.runtime.interopservices._methodbase.methodhandle.aspx

:

  • serviceCall.Method.MethodHandle, AppDomain.
  • Call.Method.GetHashCode(), , .
  • , T > serviceCall . ( , .)
+2

All Articles