1.首先添加PostSharp引用
2.添加特性(Attribute)类
[Serializable]
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true, Inherited = true)]
public class UserLogAttribute : OnMethodBoundaryAspect
{
public override void OnSuccess(MethodExecutionArgs args)
{
Arguments arguments = args.Arguments;
var user = (User)arguments[0];
}
}
3.在实现方法添加特性类
[UserLog]
public void Save(User user)
{
//保存操作
}
4.请求实现类方法Save
new UserService().Save(new User() { UserId = 1 });
5.在执行成功Save会进入到UserLogAttribute类的OnSuccess重载方法(还有很多方法都类似实现)