如何获取拦截方法的返回类型?我正在编写一个方法级别的缓存机制,我想使用 postsharp 来拦截方法调用。但是,我需要能够将存储的对象转换为原始方法类型。
public override void OnEntry(MethodExecutionArgs InterceptedItem)
{
if (_instance==null)
_instance = new CouchbaseClient();
string Key = GetCacheKey(InterceptedItem);
var CacheItem = _instance.Get(Key);
if (CacheItem != null)
{
// The value was found in cache. Don't execute the method. Return immediately.
//string StringType = (String)_instance.Get(Key+"Type");
JavaScriptSerializer jss = new JavaScriptSerializer();
InterceptedItem.ReturnValue = jss.Deserialize<Object>(CacheItem.ToString());
//Type Type = Type.GetType(StringType);
InterceptedItem.ReturnValue = (Object)InterceptedItem.ReturnValue;
// jss.Deserialize(CacheItem.ToString(), Type.GetType(StringType));
InterceptedItem.FlowBehavior = FlowBehavior.Return;
}
else
{
// The value was NOT found in cache. Continue with method execution, but store
// the cache key so that we don't have to compute it in OnSuccess.
InterceptedItem.MethodExecutionTag = Key;
}
}