Search code examples
c#castle-dynamicproxyninject-interception

Generic argument in dynamic proxy interceptor


To add and retrieve objects from memory cache I have a cache util class with these methods:

public static T GetNativeItem<T>(string itemKey)
public static void AddNativeItem(string key, object item, TimeSpan timeout)

to remove some noise from data access class I would like to use Castle Dynamic Proxy, in this particular case I would use Ninject.Extensions.Interception.

The problem is the GetNativeItem<T>(string itemKey) of the cache util, in my interceptor how can I retrieve T from invocation?

The NinjectWebCommon binding:

kernel.Bind<IMatchDataAccess>().To<MatchDataAccess>().Intercept().With<CacheInterceptor>();

The IMatchDataAccess interface have this signature:

public interface IMatchDataAccess
{
    [Cached(minutes: 10)]
    IEnumerable<DomainModel.Match> GetMatches(MatchFilterDto matchFilter);
}

and the CacheInterceptor have this 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 is 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);
      }
    }
  }

Solution

  • After some tentative I found the 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);