void IInterceptor.Intercept(InterceptCallInfo info) { if (info.InterceptType == InterceptType.BeforeGetValue || info.InterceptType == InterceptType.BeforeSetValue) { var entity = info.Target as IEntity; var entityType = entity.EntityType; var property = PropertyUnity.GetProperty(entityType, info.Member.Name); if (property == null) { return; } info.Cancel = true; switch (info.InterceptType) { case InterceptType.BeforeGetValue: var value = (entity as IPropertyAccessorBypass).GetValue(property); if (value != null && !value.IsEmpty) { info.ReturnValue = value.GetStorageValue(); } break; case InterceptType.BeforeSetValue: (entity as IPropertyAccessorBypass).SetValue(property, info.Arguments[0]); break; } } }
public void Intercept(InterceptCallInfo info) { if (info.CallMethodInfo.MethodInfo.ReturnType == typeof(int)) { info.ReturnValue = 10; } }
/// <summary> /// 更新数据缓存。 /// </summary> /// <param name="info"></param> private void UpdateDataCache(InterceptCallInfo info) { //判断是否加了 CacheSupportAttribute 特性 if (info.Member.IsDefined <CacheSupportAttribute>()) { var attr = info.Member.GetCustomAttribute <CacheSupportAttribute>(); var relationTypes = info.Member.GetCustomAttributes <CacheRelationAttribute>().Select(s => s.RelationType).ToList(); var returnType = (info.Member as MethodInfo).ReturnType; var cacheMgr = CacheManagerFactory.CreateManager(); var cacheKey = GetCacheKey(info); if (loadFromCache) { loadFromCache = false; return; } var method = MthCacheAdd.MakeGenericMethod(info.ReturnType); method.FastInvoke(cacheMgr, cacheKey, info.ReturnValue, TimeSpan.FromSeconds(attr.Expired), null); CacheKeyManager.AddKeys(returnType, cacheKey); if (relationTypes.Count > 0) { CacheKeyManager.AddKeys(relationTypes, cacheKey); } } }
/// <summary> /// 检查数据缓存。 /// </summary> /// <param name="info"></param> private void CheckDataCache(InterceptCallInfo info) { //判断是否加了 CacheSupportAttribute 特性 if (info.Member.IsDefined <CacheSupportAttribute>()) { var cacheMgr = CacheManagerFactory.CreateManager(); var cacheKey = GetCacheKey(info); try { //检查缓存管理器里有没有对应的缓存项,如果有的话直接取出来赋给 ReturnValue,然后设置 Cancel 忽略方法调用 if (cacheMgr.Contains(cacheKey)) { var returnType = (info.Member as MethodInfo).ReturnType; var method = MthCacheTryGet.MakeGenericMethod(returnType); info.ReturnValue = method.FastInvoke(cacheMgr, new object[] { cacheKey, null, null }); if (info.ReturnValue != null) { info.Cancel = true; } } } catch (Exception exp) { } } }
/// <summary> /// 更新数据缓存。 /// </summary> /// <param name="info"></param> private void UpdateDataCache(InterceptCallInfo info) { //判断是否加了 CacheSupportAttribute 特性 if (info.Member.IsDefined <CacheSupportAttribute>()) { var attr = info.Member.GetCustomAttribute <CacheSupportAttribute>(); var relationTypes = info.Member.GetCustomAttributes <CacheRelationAttribute>().Select(s => s.RelationType).ToList(); var returnType = (info.Member as MethodInfo).ReturnType; var cacheMgr = CacheManagerFactory.CreateManager(); var cacheKey = GetCacheKey(info); try { cacheMgr.Add(cacheKey, info.ReturnValue, TimeSpan.FromSeconds(attr.Expired)); CacheKeyManager.AddKeys(returnType, cacheKey); if (relationTypes.Count > 0) { CacheKeyManager.AddKeys(relationTypes, cacheKey); } } catch (Exception exp) { } } }
/// <summary> /// 重写拦截方法,输出 Attribute 中的信息。 /// </summary> /// <param name="info"></param> public override void Intercept(InterceptCallInfo info) { if (attribute != null) { Assert.AreEqual("这是一个自定义的特性", attribute.Message); } base.Intercept(info); }
/// <summary> /// 提交事务。 /// </summary> /// <param name="info"></param> private void CommitTransaction(InterceptCallInfo info) { //提交事务 if (transaction != null) { transaction.Complete(); } }
public override void Intercept(InterceptCallInfo info) { if (info.InterceptType == InterceptType.AfterMethodCall) { info.ReturnValue = Task.FromResult("hello " + (info.ReturnValue as Task <string>).Result); } base.Intercept(info); }
/// <summary> /// 重写拦截方法。 /// </summary> /// <param name="info"></param> public override void Intercept(InterceptCallInfo info) { if (info.InterceptType == InterceptType.Catching) { info.ReturnValue = 99; } base.Intercept(info); }
public override void Intercept(InterceptCallInfo info) { if (info.InterceptType == InterceptType.AfterMethodCall) { info.ReturnValue = null; } base.Intercept(info); }
public virtual void Intercept(InterceptCallInfo info) { switch (info.InterceptType) { case InterceptType.BeforeCall: BeforeCall(info); break; case InterceptType.AfterCall: AfterCall (info); break; case InterceptType.OnCatch: OnCatch (info); break; case InterceptType.OnFinally: OnFinally (info); break; } }
/// <summary> /// 打印方法的参数。 /// </summary> /// <param name="info"></param> private void PrintParameters(InterceptCallInfo info) { if (info.Arguments != null) { for (var i = 0; i < info.Arguments.Length; i++) { Console.WriteLine("参数{0}: {1}", i, info.Arguments[i]); } } }
public override int CachedMethod(int p1, int p2) { int returnValue = 0; if (_methodInfo == null) { _methodInfo = new CallMethodInfo((MethodInfo)MethodBase.GetCurrentMethod()); } InterceptCallInfo info = new InterceptCallInfo(); info.Object = this; info.CallMethodInfo = _methodInfo; info.ParameterValues[0] = p1; info.ParameterValues[1] = p2; info.ReturnValue = returnValue; info.InterceptResult = InterceptResult.Continue; info.InterceptType = InterceptType.BeforeCall; if (_interceptor == null) { _interceptor = new CacheAspect(); _interceptor.Init(_methodInfo, "MaxCacheTime=500;IsWeak=False"); } // 'BeforeCall' step checks if the method is cached. // If it is and the cache is not expired, the Intercept method populates // return value and output parameters with the cached values and // sets info.InterceptResult to InterceptResult.Return. // See the [link][file]Aspects/CacheAspect.cs[/file]CacheAspect.BeforeCall[/link] method for details. // _interceptor.Intercept(info); returnValue = (int)info.ReturnValue; if (info.InterceptResult != InterceptResult.Return) { // If the method call is not cached, target method is called. // returnValue = base.CachedMethod(p1, p2); info.ReturnValue = returnValue; info.InterceptResult = InterceptResult.Continue; info.InterceptType = InterceptType.AfterCall; // 'AfterCall' step stores parameters and return values in the cache. // See the [link][file]Aspects/CacheAspect.cs[/file]CacheAspect.AfterCall[/link] method for details. // _interceptor.Intercept(info); returnValue = (int)info.ReturnValue; } return returnValue; }
public override int CachedMethod(int p1, int p2) { int returnValue = 0; if (_methodInfo == null) { _methodInfo = new CallMethodInfo((MethodInfo)MethodBase.GetCurrentMethod()); } InterceptCallInfo info = new InterceptCallInfo(); info.Object = this; info.CallMethodInfo = _methodInfo; info.ParameterValues[0] = p1; info.ParameterValues[1] = p2; info.ReturnValue = returnValue; info.InterceptResult = InterceptResult.Continue; info.InterceptType = InterceptType.BeforeCall; if (_interceptor == null) { _interceptor = new CacheAspect(); _interceptor.Init(_methodInfo, "MaxCacheTime=500;IsWeak=False"); } // 'BeforeCall' step checks if the method is cached. // If it is and the cache is not expired, the Intercept method populates // return value and output parameters with the cached values and // sets info.InterceptResult to InterceptResult.Return. // See the [link][file]Aspects/CacheAspect.cs[/file]CacheAspect.BeforeCall[/link] method for details. // _interceptor.Intercept(info); returnValue = (int)info.ReturnValue; if (info.InterceptResult != InterceptResult.Return) { // If the method call is not cached, target method is called. // returnValue = base.CachedMethod(p1, p2); info.ReturnValue = returnValue; info.InterceptResult = InterceptResult.Continue; info.InterceptType = InterceptType.AfterCall; // 'AfterCall' step stores parameters and return values in the cache. // See the [link][file]Aspects/CacheAspect.cs[/file]CacheAspect.AfterCall[/link] method for details. // _interceptor.Intercept(info); returnValue = (int)info.ReturnValue; } return(returnValue); }
public void Intercept(InterceptCallInfo info) { if (info.InterceptType == InterceptType.BeforeMethodCall) { CheckDataCache(info); } else if (info.InterceptType == InterceptType.AfterMethodCall) { UpdateDataCache(info); } }
/// <summary> /// 重写拦截方法,输出被拦截方法的参数。 /// </summary> /// <param name="info"></param> public override void Intercept(InterceptCallInfo info) { base.Intercept(info); switch (info.InterceptType) { case InterceptType.BeforeMethodCall: case InterceptType.AfterMethodCall: PrintParameters(info); break; } }
protected override CompoundValue GetKey(InterceptCallInfo info) { var ret = base.GetKey(info); object[] keyValues = new object[ret.Count + 1]; for (int i = 0; i < ret.Count; i++) { keyValues[i] = ret[i]; } keyValues[ret.Count] = ModelUserContext.CurrentLanguage; return(new CompoundValue(keyValues)); }
/// <summary> /// 重写拦截方法。 /// </summary> /// <param name="info"></param> public override void Intercept(InterceptCallInfo info) { base.Intercept(info); if (info.Arguments != null) { for (var i = 0; i < info.Arguments.Length; i++) { Console.WriteLine("参数{0}: {1}", i, info.Arguments[i]); } } }
/// <summary> /// 检查是否需要开启事务。 /// </summary> /// <param name="info"></param> private void CheckTransaction(InterceptCallInfo info) { //判断是否加了 TransactionSupportAttribute 特性 if (info.Member.IsDefined <TransactionSupportAttribute>()) { //判断是否已经开启事务 if (EntityTransactionScope.Current == null) { transaction = new EntityTransactionScope(); } } }
public virtual void Intercept(InterceptCallInfo info) { switch (info.InterceptType) { case InterceptType.BeforeCall: BeforeCall(info); break; case InterceptType.AfterCall: AfterCall(info); break; case InterceptType.OnCatch: OnCatch(info); break; case InterceptType.OnFinally: OnFinally(info); break; } }
public void Test() { try { var info = new InterceptCallInfo(); info.ParameterValues = new object[2]; info.ParameterValues[0] = "123"; info.ParameterValues[1] = 123; throw new ApplicationException(); } catch (Exception ex) { Console.WriteLine(ex); Console.WriteLine(ex); throw; } }
public void Intercept(InterceptCallInfo info) { if (info.InterceptType == InterceptType.BeforeMethodCall) { CheckTransaction(info); } //执行方法后 else if (info.InterceptType == InterceptType.AfterMethodCall) { CommitTransaction(info); } //最终完成后 else if (info.InterceptType == InterceptType.Finally) { if (transaction != null) { transaction.Dispose(); transaction = null; } } }
public void Test() { TestClass t = (TestClass)TypeAccessor.CreateInstance(typeof(TestClass)); for (int i = 0; i < 10; i++) { t.Test(); } MethodCallCounter counter = CounterAspect.GetCounter(typeof(TestClass).GetMethod("Test")); Assert.AreEqual(10, counter.TotalCount); Console.WriteLine(counter.TotalTime); new Thread(new ThreadStart(t.LongTest)).Start(); Thread.Sleep(20); lock (CounterAspect.Counters.SyncRoot) foreach (MethodCallCounter c in CounterAspect.Counters) { Console.WriteLine("{0}.{1,-10} | {2,2} | {3,2} | {4}", c.MethodInfo.DeclaringType.Name, c.MethodInfo.Name, c.TotalCount, c.CurrentCalls.Count, c.TotalTime); lock (c.CurrentCalls.SyncRoot) for (int i = 0; i < c.CurrentCalls.Count; i++) { InterceptCallInfo ci = (InterceptCallInfo)c.CurrentCalls[i]; IPrincipal pr = ci.CurrentPrincipal; Console.WriteLine("{0,15} | {1}", pr == null? "***" : pr.Identity.Name, DateTime.Now - ci.BeginCallTime); } } }
/// <summary> /// 拦截到的信息。 /// </summary> /// <param name="info"></param> public virtual void Intercept(InterceptCallInfo info) { switch (info.InterceptType) { case InterceptType.BeforeMethodCall: Console.WriteLine("调用 {0} 方法之前", info.Member.Name); break; case InterceptType.AfterMethodCall: Console.WriteLine("调用 {0} 方法之后", info.Member.Name); break; case InterceptType.BeforeGetValue: Console.WriteLine("获取 {0} 属性之前", info.Member.Name); break; case InterceptType.AfterGetValue: Console.WriteLine("获取 {0} 属性之后", info.Member.Name); break; case InterceptType.BeforeSetValue: Console.WriteLine("设置 {0} 属性之前", info.Member.Name); break; case InterceptType.AfterSetValue: Console.WriteLine("设置 {0} 属性之后", info.Member.Name); break; case InterceptType.Catching: Console.WriteLine("{0} 发生了异常", info.Member.Name); break; case InterceptType.Finally: Console.WriteLine("{0} Finally", info.Member.Name); break; } }
private static string GetCacheKey(InterceptCallInfo info) { var sb = new StringBuilder(); sb.Append(info.Member.DeclaringType.Name); sb.Append("-"); sb.Append(info.Member.Name); sb.Append(":"); var parameters = (info.Member as MethodInfo).GetParameters(); for (var i = 0; i < info.Arguments.Length; i++) { sb.AppendFormat("&{0}=", parameters[i].Name); if (info.Arguments[i] != null) { if (typeof(IEnumerable).IsAssignableFrom(parameters[i].ParameterType) && typeof(string) != parameters[i].ParameterType) { sb.Append("["); foreach (var item in (IEnumerable)info.Arguments[i]) { sb.AppendFormat("{0}-", item.ToString()); } sb.Append("]"); } else { sb.Append(info.Arguments[i]); } } } return(sb.ToString()); }
protected virtual void AfterCall(InterceptCallInfo info) { }
protected virtual void BeforeCall(InterceptCallInfo info) {}
protected virtual void AfterCall (InterceptCallInfo info) {}
protected virtual void OnCatch (InterceptCallInfo info) {}
protected virtual void OnFinally (InterceptCallInfo info) {}
public override void TestMethod() { if (_methodInfo == null) { _methodInfo = new CallMethodInfo((MethodInfo)MethodBase.GetCurrentMethod()); } InterceptCallInfo info = new InterceptCallInfo(); try { info.Object = this; info.CallMethodInfo = _methodInfo; info.InterceptResult = InterceptResult.Continue; info.InterceptType = InterceptType.BeforeCall; if (_interceptor == null) { _interceptor = new CounterAspect(); _interceptor.Init(_methodInfo, null); } // 'BeforeCall' creates or gets a counter for the method and // registers the current call. // See the [link][file]Aspects/CounterAspect.cs[/file]CounterAspect.BeforeCall[/link] method for details. // _interceptor.Intercept(info); if (info.InterceptResult != InterceptResult.Return) { // Target method call. // base.TestMethod(); } } catch (Exception exception) { info.Exception = exception; info.InterceptResult = InterceptResult.Continue; info.InterceptType = InterceptType.OnCatch; // 'OnCatch' is required to count calls with exceptions. // _interceptor.Intercept(info); if (info.InterceptResult != InterceptResult.Return) { throw; } } finally { info.InterceptResult = InterceptResult.Continue; info.InterceptType = InterceptType.OnFinally; // 'OnFinally' step adds statistic to the method counter. // See the [link][file]Aspects/CounterAspect.cs[/file]CounterAspect.OnFinally[/link] method for details. // _interceptor.Intercept(info); } }
public void Intercept(InterceptCallInfo info) { Console.WriteLine(info.InterceptType); }
protected override void BeforeCall(InterceptCallInfo info) { info.Items["ReturnValue"] = info.ReturnValue; }
protected override void AfterCall(InterceptCallInfo info) { info.ReturnValue = (int)info.ReturnValue + (int)info.Items["ReturnValue"]; }
protected virtual void BeforeCall(InterceptCallInfo info) { }
protected virtual void OnCatch(InterceptCallInfo info) { }
protected virtual void OnFinally(InterceptCallInfo info) { }