C# 动态代理拦截器中的泛型参数

C# 动态代理拦截器中的泛型参数,c#,castle-dynamicproxy,ninject-interception,C#,Castle Dynamicproxy,Ninject Interception,要从内存缓存中添加和检索对象,我有一个包含以下方法的cache util类: public static T GetNativeItem<T>(string itemKey) public static void AddNativeItem(string key, object item, TimeSpan timeout) IMatchDataAccess接口具有以下签名: public interface IMatchDataAccess { [Cached(minut

要从内存缓存中添加和检索对象,我有一个包含以下方法的cache util类:

public static T GetNativeItem<T>(string itemKey)
public static void AddNativeItem(string key, object item, TimeSpan timeout)
IMatchDataAccess接口具有以下签名:

public interface IMatchDataAccess
{
    [Cached(minutes: 10)]
    IEnumerable<DomainModel.Match> GetMatches(MatchFilterDto matchFilter);
}
CacheInterceptor具有以下实现:

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);
      }
    }
  }
经过一些尝试后,使用反射:

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);
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);