PS用了我们熟知的东西,而没有引入新名词,这是它聪明的地方。看表格右边的一组概念,事件(还不太理解为什么说是meta),事件处理器,aspect=定制的attribute, 唯一新鲜的是multicasting,实际上可以理解为一个通配表达式。
下面再用一个简单例子:
public class LogAttribute : OnMethodBoundaryAspect
{
public override void OnEntry (MethodExecutionEventArgs eventArgs )
{ Console . WriteLine (" Entering the method {0}." , eventArgs . Method );
}
public override void OnExit (MethodExecutionEventArgs eventArgs )
{
Console . WriteLine (" Leaving the method {0}." , eventArgs . Method );
}
}
class Program
{
[Log]
static void Main ()
{
Console . WriteLine (" Hello , world .");
}
}
//multicasting 说明对MyApp . BusinessObjects命名空间使用Log
[ assembly : Log(
AttributeTargetTypes =
" MyApp . BusinessObjects .*" ,
AttributeTargetMemberAttributes =
MulticastAttributes . Public )]
{
public override void OnEntry (MethodExecutionEventArgs eventArgs )
{ Console . WriteLine (" Entering the method {0}." , eventArgs . Method );
}
public override void OnExit (MethodExecutionEventArgs eventArgs )
{
Console . WriteLine (" Leaving the method {0}." , eventArgs . Method );
}
}
class Program
{
[Log]
static void Main ()
{
Console . WriteLine (" Hello , world .");
}
}
//multicasting 说明对MyApp . BusinessObjects命名空间使用Log
[ assembly : Log(
AttributeTargetTypes =
" MyApp . BusinessObjects .*" ,
AttributeTargetMemberAttributes =
MulticastAttributes . Public )]
从这例子看非常简明清晰,的确易学易用。