c# - Generic argument in dynamic proxy interceptor -


to add , retrieve objects memory cache have cache util class these methods:

public static t getnativeitem<t>(string itemkey) public static void addnativeitem(string key, object item, timespan timeout) 

to remove noise data access class use castle dynamic proxy, in particular case use ninject.extensions.interception.

the problem getnativeitem<t>(string itemkey) of cache util, in interceptor how can retrieve t invocation?

the ninjectwebcommon binding:

kernel.bind<imatchdataaccess>().to<matchdataaccess>().intercept().with<cacheinterceptor>(); 

the imatchdataaccess interface have signature:

public interface imatchdataaccess {     [cached(minutes: 10)]     ienumerable<domainmodel.match> getmatches(matchfilterdto matchfilter); } 

and cacheinterceptor have implementation:

public class cacheinterceptor : iinterceptor   {     public void intercept(iinvocation invocation)     {       var cachedattr = invocation.request.method.getattribute<cachedattribute>();        var p = invocation;       if (cachedattr == null)       {         invocation.proceed();         return;       }        var cachekey = string.concat(invocation.request.method.returntype.tostring(), ".", invocation.request.method.name, "(", string.join(", ", invocation.request.arguments), ")");        /*           problem here       */        var p = invocation.request.method.returntype;       var objincache = cacheutil.getnativeitem<p>(cachekey);         if (objincache != null)         invocation.returnvalue = objincache;        else       {         invocation.proceed();         var timeout = cachedattr.minutes > 0 ? new timespan(0, cachedattr.minutes, 0) : new timespan(0, 60, 0);         cacheutil.addnativeitem(cachekey, invocation.returnvalue, timeout);       }     }   } 

after tentative i found solution here, using reflection:

var method = typeof(cacheutil).getmethod("getnativeitem");       var gmethod = method.makegenericmethod(invocation.request.method.returntype);        var objincache = gmethod.invoke(typeof(cacheutil), bindingflags.static, null, new object[] { cachekey }, cultureinfo.invariantculture); 

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -