相比于对类方法的拦截,对接口方法的拦截为我们的架构设计方面提供了更大的自由度。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CastleAOPTestB.Lib
{
public interface IPerson
{
void SayHello();
void SayName(string name);
string GotoSchool(string schoolName, string grade, string classes);
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CastleAOPTestB.Lib;
namespace CastleAOPTestB.Model
{
public class Person : IPerson
{
public void SayHello()
{
Console.WriteLine("您好。");
}
public void SayName(string name)
{
Console.WriteLine("我是{0}。", name);
}
public string GotoSchool(string schoolName, string grade, string classes)
{
return string.Format("我就读的学校是:{0},我的班级是:{1} {2}", schoolName, grade, classes);
}
}
}
两个都很普通的接口和实现两个都很普通的接口和实现两个都很普通的接口和实现两个都很普通的接口和实现。
单纯的拦截器是指,调用时不需要实现类的拦截器。是本例的测试案例之一。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using Castle.DynamicProxy;
namespace CastleAOPTestB.Proxy
{
public class SimpleInterceptor : IInterceptor
{
public void Intercept(IInvocation invocation)
{
Console.WriteLine("当前方法名:{0}", invocation.Method.Name);
string signature = string.Empty;
//在这个循环中通过反射计算出方法签名
foreach(ParameterInfo info in invocation.Method.GetParameters())
{
string paramSignature = string.Format("{0} {1}",info.ParameterType.Name, info.Name);
if (string.IsNullOrEmpty(signature))
signature = paramSignature;
else signature = signature + "," + paramSignature;
}
//输出方法签名
Console.WriteLine("方法签名:{0} ({1})", invocation.Method.ReturnType.Name, signature);
//直接设置返回值为函数名称
invocation.ReturnValue = invocation.Method.Name;
}
}
}
本拦截器在调用时需要使用到实现类Person。这是本例的测试案例之二。
有点类似上一节类拦截器,但一个是继承类Castle.DynamicProxy.StandardInterceptor,一个是实现接口Castle.DynamicProxy.IInterceptor。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using Castle.DynamicProxy;
namespace CastleAOPTestB.Proxy
{
public class ModelInterceptor : IInterceptor
{
public void Intercept(IInvocation invocation)
{
Console.WriteLine("当前方法名:{0}", invocation.Method.Name);
invocation.Proceed();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Castle.DynamicProxy;
using CastleAOPTestB.Lib;
using CastleAOPTestB.Model;
using CastleAOPTestB.Proxy;
namespace CastleAOPTestB.Run
{
class Program
{
static void Main(string[] args)
{
Call_AOP_Interface();//单纯的拦截器,调用时不需要实现类的拦截器
Call_AOP_Model();//需要实现类Person的拦截器
Console.ReadLine();
}
///
/// 本例的测试案例之一:单纯的拦截器,调用时不需要实现类的拦截器
///
private static void Call_AOP_Interface()
{
Console.WriteLine();
Console.WriteLine("=====本例的测试案例之一:单纯的拦截器,调用时不需要实现类的拦截器=====");
ProxyGenerator generator = new ProxyGenerator();
//采用默认的基类(Object)
IPerson person = generator.CreateInterfaceProxyWithoutTarget(new SimpleInterceptor());
DisplayMessage(person);
ProxyGenerator proxyGenerator = new ProxyGenerator();
ProxyGenerationOptions options = new ProxyGenerationOptions();
//改变接口对象的基类为MarshalByRefObject
options.BaseTypeForInterfaceProxy = typeof(MarshalByRefObject);
IPerson person1 = (IPerson)proxyGenerator.CreateInterfaceProxyWithoutTarget(
typeof(IPerson),
null, options,
new SimpleInterceptor());
DisplayMessage(person1);
}
///
/// 本例的测试案例之二:需要实现类Person的拦截器
///
private static void Call_AOP_Model()
{
Console.WriteLine();
Console.WriteLine("=====本例的测试案例之二:需要实现类Person的拦截器=====");
ProxyGenerator generator = new ProxyGenerator();
//采用默认的基类(Object)
IPerson person = generator.CreateInterfaceProxyWithTarget(new Person(), new ModelInterceptor());
DisplayMessage(person);
}
private static void DisplayMessage(IPerson person)
{
Console.WriteLine("当前基类:{0}", person.GetType().BaseType);
Console.WriteLine();
person.SayHello();
Console.WriteLine();
person.SayName("天涯人");
Console.WriteLine();
Console.WriteLine("返回值:{0}",person.GotoSchool("华师附小", "三年级", "三班"));
}
}
}
http://download.csdn.net/detail/liuweitoo/4752659