AOP介绍面向切面编程(Aspect Oriented Programming,英文缩写为AOP),通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。AOP是OOP的延续,是软件开发中的一个热点.常用于:AuthenticationCachingLazy loadingTransactions普通类123456789class
Person : MarshalByRefObject
{
public
string
Say()
{
const
string
str =
"Person's say is called"
;
Console.WriteLine(str);
return
str;
}
}
代理类12345678910111213141516public
class
Proxy
where
T :
new
()
{
private
object
_obj;
public
Proxy(
object
obj)
:
base
(
typeof
(T))
{
_obj = obj;
}
public
override
IMessage Invoke(IMessage msg)
{
Console.WriteLine(
"{0}:Invoke前"
, DateTime.Now);
var
ret = ((IMethodCallMessage)msg).MethodBase.Invoke(_obj,
null
);
Console.WriteLine(
"{0}:Invoke后"
, DateTime.Now);
return
new
ReturnMessage(ret,
null
, 0,
null
,
null
);
}
}
执行12345678910static
void
Main(
string
[] args)
{
var
per =
new
Proxy
new
Person()).GetTransparentProxy()
as
Person;
if
(per !=
null
)
{
var
str = per.Say();
Console.WriteLine(
"返回值:"
+ str);
}
Console.ReadKey();
}
AOP有动态代理和静态IL织入.本节主要介绍动态代理方式,静态可参考PostSharp.原理:本质是创建继承原来类的代理类.重写虚方法实现AOP功能.只需引用:Install-Package Castle.Core(在Castle的2.5以上版本,已经将 Castle.DynamicProxy2.dll 里有内容,集成到 Castle.Core.dll 中。)Simple Class12345678910111213141516171819public
abstract
class
Person
{
public
virtual
void
SayHello()
{
Console.WriteLine(
"我是{0}方法"
,
"SayHello"
);
}
public
virtual
void
SayName(
string
name)
{
Console.WriteLine(
"我是{0}方法,参数值:{1}"
,
"SayName"
, name);
}
public
abstract
void
AbstactSayOther();
public
void
SayOther()
{
Console.WriteLine(
"我是{0}方法"
,
"SayOther"
);
}
}
interceptor12345678910111213141516171819202122public
class
SimpleInterceptor : StandardInterceptor
{
protected
override
void
PreProceed(IInvocation invocation)
{
Console.WriteLine(
"拦截器调用方法前,方法名是:{0}。"
, invocation.Method.Name);
}
protected
override
void
PerformProceed(IInvocation invocation)
{
Console.WriteLine(
"拦截器开始调用方法,方法名是:{0}。"
, invocation.Method.Name);
var
attrs = invocation.MethodInvocationTarget.Attributes.HasFlag(MethodAttributes.Abstract);
//过滤abstract方法
if
(!attrs)
{
base
.PerformProceed(invocation);
//此处会调用真正的方法 invocation.Proceed();
}
}
protected
override
void
PostProceed(IInvocation invocation)
{
Console.WriteLine(
"拦截器调用方法后,方法名是:{0}。"
, invocation.Method.Name);
}
}
Main1234567891011121314151617181920212223static
void
Main(
string
[] args)
{
var
generator =
new
ProxyGenerator();
//实例化【代理类生成器】
var
interceptor =
new
SimpleInterceptor();
//实例化【拦截器】
//使用【代理类生成器】创建Person对象,而不是使用new关键字来实例化
var
person = generator.CreateClassProxy
Console.WriteLine(
"当前类型:{0},父类型:{1}"
, person.GetType(), person.GetType().BaseType);
Console.WriteLine();
person.SayHello();
//拦截
Console.WriteLine();
person.SayName(
"Never、C"
);
//拦截
Console.WriteLine();
person.SayOther();
//普通方法,无法拦截
person.AbstactSayOther();
//抽象方法,可以拦截
Console.ReadLine();
}
特性式AOP12345678910111213public
interface
IPerson
{
void
Say();
}
[Interceptor(
typeof
(LogInterceptor))]
public
class
Person : IPerson
{
public
void
Say()
{
Console.WriteLine(
"Person's Say Method is called!"
);
}
}
123456789public
class
LogInterceptor : IInterceptor
{
public
void
Intercept(IInvocation invocation)
{
Console.WriteLine(
"{0}:拦截{1}方法{2}前,"
, DateTime.Now.ToString(
"O"
), invocation.InvocationTarget.GetType().BaseType, invocation.Method.Name);
invocation.Proceed();
Console.WriteLine(
"{0}:拦截{1}方法{2}后,"
, DateTime.Now.ToString(
"O"
), invocation.InvocationTarget.GetType().BaseType, invocation.Method.Name);
}
}
1234567891011static
void
Main(
string
[] args)
{
using
(
var
container =
new
WindsorContainer())
{
container.Register(Component.For
container.Register(Component.For
var
person = container.Resolve
person.Say();
}
Console.ReadKey();
}
非侵入式AOP123456789101112public
interface
IPerson
{
void
Say();
}
public
class
Person : IPerson
{
public
void
Say()
{
Console.WriteLine(
"Person's Say Method is called!"
);
}
}
123456789101112131415161718192021internal
static
class
LogInterceptorRegistrar
{
public
static
void
Initialize(WindsorContainer container)
{
container.Kernel.ComponentRegistered += Kernel_ComponentRegistered;
}
private
static
void
Kernel_ComponentRegistered(
string
key, IHandler handler)
{
handler.ComponentModel.Interceptors.Add(
new
InterceptorReference(
typeof
(LogInterceptor)));
}
}
public
class
LogInterceptor : IInterceptor
{
public
void
Intercept(IInvocation invocation)
{
Console.WriteLine(
"{0}:拦截{1}方法{2}前,"
, DateTime.Now.ToString(
"O"
), invocation.InvocationTarget.GetType().BaseType, invocation.Method.Name);
invocation.Proceed();
Console.WriteLine(
"{0}:拦截{1}方法{2}后,"
, DateTime.Now.ToString(
"O"
), invocation.InvocationTarget.GetType().BaseType, invocation.Method.Name);
}
}
123456789101112static
void
Main(
string
[] args)
{
using
(
var
container =
new
WindsorContainer())
{
container.Register(Component.For
//先注入拦截器
LogInterceptorRegistrar.Initialize(container);
container.Register(Component.For
var
person = container.Resolve
person.Say();
}
Console.ReadKey();
}
Install-Package Autofac.Aop通过特性标签绑定1234567891011121314151617181920212223class
LogInterceptor : IInterceptor
{
public
void
Intercept(IInvocation invocation)
{
Console.WriteLine(
"{0}:拦截{1}方法{2}前,"
, DateTime.Now.ToString(
"O"
), invocation.InvocationTarget.GetType().BaseType, invocation.Method.Name);
invocation.Proceed();
Console.WriteLine(
"{0}:拦截{1}方法{2}后,"
, DateTime.Now.ToString(
"O"
), invocation.InvocationTarget.GetType().BaseType, invocation.Method.Name);
}
}
public
interface
IPerson
{
void
Say();
}
[Intercept(
typeof
(LogInterceptor))]
public
class
Person : IPerson
{
public
void
Say()
{
Console.WriteLine(
"Person's Say Method is called!"
);
}
}
启用拦截器执行1234567891011static
void
Main(
string
[] args)
{
var
builder =
new
ContainerBuilder();
builder.RegisterType
builder.RegisterType
using
(
var
container = builder.Build())
{
container.Resolve
}
Console.ReadLine();
}
或采用非侵入性方法(去掉class上的特性仍可以)1234567891011static
void
Main(
string
[] args)
{
var
builder =
new
ContainerBuilder();
builder.RegisterType
typeof
(LogInterceptor));
builder.RegisterType
using
(
var
container = builder.Build())
{
container.Resolve
}
Console.ReadLine();
}
Unity默认提供了三种拦截器:TransparentProxyInterceptor、InterfaceInterceptor、VirtualMethodInterceptor。TransparentProxyInterceptor:代理实现基于.NET Remoting技术,它可拦截对象的所有函数。缺点是被拦截类型必须派生于MarshalByRefObject。InterfaceInterceptor:只能对一个接口做拦截,好处时只要目标类型实现了指定接口就可以拦截。VirtualMetho免费云主机域名dInterceptor:对virtual函数进行拦截。缺点是如果被拦截类型没有virtual函数则无法拦截,这个时候如果类型实现了某个特定接口可以改用Install-Package Unity.Interception12345678910111213public
class
MyHandler : ICallHandler
{
public
int
Order {
get
;
set
; }
//这是ICallHandler的成员,表示执行顺序
public
IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)
{
Console.WriteLine(
"方法执行前"
);
//这之前插入方法执行前的处理
var
retvalue = getNext()(input, getNext);
//在这里执行方法
//这之后插入方法执行后的处理
Console.WriteLine(
"方法执行后"
);
return
retvalue;
}
}
1234567public
class
MyHandlerAttribute : HandlerAttribute
{
public
override
ICallHandler CreateHandler(IUnityContainer container)
{
return
new
MyHandler();
//返回MyHandler
}
}
12345678910111213public
interface
IPerson
{
void
Say();
}
[MyHandler]
public
class
Person : IPerson
{
public
virtual
void
Say()
{
Console.WriteLine(
"Person's Say Method is called!"
);
}
}
123456789101112131415161718static
void
Main(
string
[] args)
{
using
(
var
container =
new
UnityContainer())
{
container.AddNewExtension
//1.TransparentProxyInterceptor
//container.Configure
//2.InterfaceInterceptor (使用1,2,3均可,这种侵入性最小)
container.Configure
new
InterfaceInterceptor());
//3.VirtualMethodInterceptor
//container.Configure
container.RegisterType
container.Resolve
}
Console.ReadKey();
}
这篇文章主要介绍“linux和bsd间有哪些区别”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“linux和bsd间有哪些区别”文章能帮助大家解决问题。 区别:1、linux只是一个系统内核,BSD是完整操作系统。2、L…
免责声明:本站发布的图片视频文字,以转载和分享为主,文章观点不代表本站立场,本站不承担相关法律责任;如果涉及侵权请联系邮箱:360163164@qq.com举报,并提供相关证据,经查实将立刻删除涉嫌侵权内容。