Spring.net方法的替换

1.为什么有时候你再执行某个方法的时候比如某个操作
a.权限验证 b.任务执行
当我执行到这个方法的时候,我可以先验证权限,如果验证不通过则替换到另一个方法去执行
public class MyValueCalculator {
 public virtual string ComputeValue(string input) {
 // ... some real code
 }
 //
2.代码实现
替换类(含替换方法)的定义
/// <summary>
/// Meant to be used to override the existing ComputeValue(string)
/// implementation in MyValueCalculator.
/// </summary>
public class ReplacementComputeValue : IMethodReplacer
{
public object Implement(object target, MethodInfo method, object[] arguments)
 {
 // get the input value, work with it, and return a computed result...
 string value = (string) arguments[0];
 // compute...
 return result;
 }
}

3.
<object id="myValueCalculator" type="Examples.MyValueCalculator, ExampleAssembly">
 <!-- arbitrary method replacement -->
 <replaced-method name="ComputeValue" replacer="replacementComputeValue">
 <arg-type match="String"/>
 </replaced-method>
</object>
<object id="replacementComputeValue" type="Examples.ReplacementComputeValue, ExampleAssembly"/>

 

posted on 2016-09-29 11:53  听哥哥的话  阅读(278)  评论(0编辑  收藏  举报

导航