using System;
using System.Linq;
using Castle.DynamicProxy; namespace AopTest
{
class AopTest
{
static public void Test()
{
var proxyGenerator = new ProxyGenerator();
IUserService user = proxyGenerator.CreateClassProxy<UserService>(new UserRigthCheckProxy());
user.GetUser("admin");
user.GetUser("guest");
user.UpdateUser("admin");
user.UpdateUser("guest");
}
} public interface IUserService
{
void GetUser(string userId);
void UpdateUser(string userId);
} public class UserService : IUserService
{
public virtual void GetUser(string userId)
{
Console.WriteLine(userId + ",Welcome!");
}
public virtual void UpdateUser(string userId)
{
Console.WriteLine(userId + ",Updated!");
}
} public class UserRigthCheckProxy : IInterceptor
{
private static readonly string[] RightOperations = new string[] { "UpdateUser" };
public void Intercept(IInvocation invocation)
{
if (invocation.Arguments.Length > && invocation.Arguments[] is string)
{
if (RightOperations.Contains(invocation.Method.Name))
{
var userId = (string) invocation.Arguments[];
if (userId == "admin")
{
Console.WriteLine("有权限操作 ");
invocation.Proceed();
}
else
{
Console.WriteLine("{0},您没有权限操作", userId);
}
}
else
{
Console.WriteLine("有权限操作 ");
invocation.Proceed();
}
}
else
{
invocation.Proceed();
}
}
}
}

最新文章

  1. js计算在线时长
  2. Python核心编程(切片索引的更多内容)
  3. hibernate annotation注解方式来处理映射关系
  4. ubuntu装机后的一些零散配置
  5. 黑马程序员——Objective-c特性
  6. php单引号、双引号与数据库
  7. XML的四种解析器原理及性能比较
  8. 【WinRT】【译】【加工】在 XAML 中制作圆形图片
  9. Android数据加密概述及多种加密方式 聊天记录及账户加密 提供高质量的数据保护
  10. css进度条
  11. affiliate的使用方式
  12. JavaWeb学习(二十二)———EL表达式
  13. 传统DOM事件处理程序
  14. mysql-sql语言参考
  15. JSP内置对象——application对象
  16. bzoj3503 和谐矩阵
  17. 牛客OI周赛3-提高组-B-1408[dp]
  18. c++ 在线编译
  19. 【软件安装】Xshell + XFtp
  20. 三层神经网络自编码算法推导和MATLAB实现 (转载)

热门文章

  1. OpenCV的Rect矩形类用法
  2. Python ---- super()使用
  3. Part3_lesson4---协处理器访问指令
  4. Spring--入门第二天
  5. this与$(this)的区别
  6. What’s the Difference Between a Value Provider and Model Binder?
  7. 解决Spring Boot(2.1.3.RELEASE)整合spring-data-elasticsearch3.1.5.RELEASE报NoNodeAvailableException[None of the configured nodes are available
  8. CentOS下配置防火墙 配置nat转发服务
  9. HTTP总结
  10. vi/vim使用总结