动态代理拦截器中的通用参数
Generic argument in dynamic proxy interceptor
要从内存缓存中添加和检索对象,我有一个缓存实用程序 class,使用这些方法:
public static T GetNativeItem<T>(string itemKey)
public static void AddNativeItem(string key, object item, TimeSpan timeout)
从数据访问中消除一些噪音 class 我想使用 Castle Dynamic Proxy, in this particular case I would use Ninject.Extensions.Interception。
问题是缓存实用程序的 GetNativeItem<T>(string itemKey)
,在我的拦截器中,我如何从 invocation
中检索 T
?
NinjectWebCommon 绑定:
kernel.Bind<IMatchDataAccess>().To<MatchDataAccess>().Intercept().With<CacheInterceptor>();
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);
}
}
}
经过一些尝试 I found the solution here,使用反射:
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);
要从内存缓存中添加和检索对象,我有一个缓存实用程序 class,使用这些方法:
public static T GetNativeItem<T>(string itemKey)
public static void AddNativeItem(string key, object item, TimeSpan timeout)
从数据访问中消除一些噪音 class 我想使用 Castle Dynamic Proxy, in this particular case I would use Ninject.Extensions.Interception。
问题是缓存实用程序的 GetNativeItem<T>(string itemKey)
,在我的拦截器中,我如何从 invocation
中检索 T
?
NinjectWebCommon 绑定:
kernel.Bind<IMatchDataAccess>().To<MatchDataAccess>().Intercept().With<CacheInterceptor>();
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);
}
}
}
经过一些尝试 I found the solution here,使用反射:
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);