Aop之使用Castle动态代理实现对方法的拦截
2024-10-10 22:40:34
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();
}
}
}
}
最新文章
- js计算在线时长
- Python核心编程(切片索引的更多内容)
- hibernate annotation注解方式来处理映射关系
- ubuntu装机后的一些零散配置
- 黑马程序员——Objective-c特性
- php单引号、双引号与数据库
- XML的四种解析器原理及性能比较
- 【WinRT】【译】【加工】在 XAML 中制作圆形图片
- Android数据加密概述及多种加密方式 聊天记录及账户加密 提供高质量的数据保护
- css进度条
- affiliate的使用方式
- JavaWeb学习(二十二)———EL表达式
- 传统DOM事件处理程序
- mysql-sql语言参考
- JSP内置对象——application对象
- bzoj3503 和谐矩阵
- 牛客OI周赛3-提高组-B-1408[dp]
- c++ 在线编译
- 【软件安装】Xshell + XFtp
- 三层神经网络自编码算法推导和MATLAB实现 (转载)
热门文章
- OpenCV的Rect矩形类用法
- Python ---- super()使用
- Part3_lesson4---协处理器访问指令
- Spring--入门第二天
- this与$(this)的区别
- What’s the Difference Between a Value Provider and Model Binder?
- 解决Spring Boot(2.1.3.RELEASE)整合spring-data-elasticsearch3.1.5.RELEASE报NoNodeAvailableException[None of the configured nodes are available
- CentOS下配置防火墙 配置nat转发服务
- HTTP总结
- vi/vim使用总结