RealProxy实现AOP编程(1)
Program.cs
class Program
{
static void Main(string[] args)
{
User user = " };
var processor = TransparentProxy.Create<UserProcessor>();
processor.Shopping(user);
Console.ReadLine();
}
}
User.cs
public class User
{
public string Name { get; set; }
public string Password { get; set; }
}
IUserProcessor.cs
public interface IUserProcessor
{
void Shopping(User user);
}
UserProcessor.cs
public class UserProcessor : MarshalByRefObject, IUserProcessor
{
public void Shopping(User user)
{
Console.WriteLine("买呀,买呀,买桃子!");
}
}
UserProxyProcessor.cs
class UserProxyProcessor<T> : RealProxy
{
private T processor;
public UserProxyProcessor(T processor) :
base(typeof(T))//不能丢
{
this.processor = processor;
}
public override IMessage Invoke(IMessage msg)
{
PreShopping();
IMethodCallMessage message = (IMethodCallMessage)msg;
object result = message.MethodBase.Invoke(processor, message.Args);
PostShopping();
], , null, message);
}
private void PreShopping()
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("买东西前,判断是否注册了!");
Console.ResetColor();
}
private void PostShopping()
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("买完东西后,写个日志文件!");
Console.ResetColor();
}
}
TransparentProxy.cs
public static class TransparentProxy
{
public static T Create<T>()
{
T instance = Activator.CreateInstance<T>();
UserProxyProcessor<T> processor = new UserProxyProcessor<T>(instance);
T transparentProxy = (T)processor.GetTransparentProxy();
return transparentProxy;
}
}
这里我们必须让 UserProcessor 类继承 MarshalByRefObject。很不好!
RealProxy实现AOP编程(1)的更多相关文章
- RealProxy实现AOP编程(2)
稍微变化一下!注意区别. Program.cs class Program { static void Main(string[] args) { User user = " }; var ...
- .Net中的RealProxy实现AOP
序言 这个AOP要从我们公司的一个事故说起,前段时间公司的系统突然在乌云中出现,数据被泄露的一览无余,乌云上显示是SQL注入攻击.呵,多么贴近生活的一个露洞,可谓是人尽皆知啊.然而却华丽丽的给拉我们一 ...
- 【原】iOS动态性(三) Method Swizzling以及AOP编程:在运行时进行代码注入
概述 今天我们主要讨论iOS runtime中的一种黑色技术,称为Method Swizzling.字面上理解Method Swizzling可能比较晦涩难懂,毕竟不是中文,不过你可以理解为“移花接木 ...
- 使用spring方式来实现aop编程
1:什么是aop? Aspect Oriented Programming 面向切面编程 在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译 ...
- Spring学习笔记之四----基于Annotation的Spring AOP编程
你能使用@Aspect annotation将某个Java类标注为Aspect,这个Aspect类里的所有公有方法都可以成为一个Advice,Spring提供了5个Annotation去将某个方法标注 ...
- 聊Javascript中的AOP编程
Duck punch 我们先不谈AOP编程,先从duck punch编程谈起. 如果你去wikipedia中查找duck punch,你查阅到的应该是monkey patch这个词条.根据解释,Mon ...
- 基于ASP.NET MVC的热插拔模块式开发框架(OrchardNoCMS)--AOP编程
AOP编程在目前来说好像是大家都比较喜欢的.ASP.NET MVC中的Filter就是使用AOP实现的配置器模式.AOP在编码中的应用主要有如下几个方面: 日志记录,跟踪,优化和监控 事务的处理 持久 ...
- struts2.1笔记03:AOP编程和拦截器概念的简介
1.AOP编程 AOP编程,也叫面向切面编程(也叫面向方面):Aspect Oriented Programming(AOP),是目前软件开发中的一个热点,也是Spring框架中的一个重要内容.利用A ...
- Method Swizzling以及AOP编程:在运行时进行代码注入-备用
概述 今天我们主要讨论iOS runtime中的一种黑色技术,称为Method Swizzling.字面上理解Method Swizzling可能比较晦涩难懂,毕竟不是中文,不过你可以理解为“移花接木 ...
随机推荐
- Android数据存储方式
Android提供了5种方式存储数据: 1.使用SharedPreferences存储数据:它是Android提供的用来存储一些简单配置信息的一种机制,采用了XML格式将数据存储到设备中.只能在同一个 ...
- 学习solr
1.下载地址 http://apache.opencas.org/lucene/solr/
- 部署私有的Nuget服务器
1.查看官方的部署文档:http://docs.nuget.org/Create/Hosting-Your-Own-NuGet-Feeds 2.使用开源的项目:https://github.com/h ...
- iOS开发_数据存储方式
对于数据持久化的问题,博主并不准备在博文内放很多的代码进行更深一步解释,只是简单的介绍一下四种数据持久化,如有时间,会另外针对各个数据持久化的方法进行更进一步的阐述. 直接进入主题: 〈1.NSUse ...
- sql基础语句(技巧)
1.压缩数据库 dbcc shrinkdatabase(dbname) 2.转移数据库给新用户已存在用户权限 exec sp_change_users_login'update_one','newga ...
- SpringMVC 表单标签 & 处理静态资源
使用 Spring 的表单标签 通过 SpringMVC 的表单标签可以实现将模型数据中的属性和 HTML 表单元素相绑定,以实现表单数据更便捷编辑和表单值的回显. form 标签 一般情况下,通过 ...
- 【转】Memcached安装
解析:Memcached是什么? Memcached是由Danga Interactive开发的,高性能的,分布式的内存对象缓存系统,用于在动态应用中减少数据库负载,提升访问速度. 一.软件版本 ...
- 例子:Database - Linq to sql
DataContext类型(数据上下文)是System.Data.Linq命名空间下的重要类型,用于把查询句法翻译成SQL语句,以及把数据从数据库返回给调用方和把实体的修改写入数据库. DataCon ...
- C++ 之 auto_ptr and shared_ptr
1.auto_ptr 这个所谓的只能指针有点鸡肋! 没有引用计数,而且还有一个所有权转移的情况! 当所有权转移后,以前的auto_ptr将会成为null 2.shared_ptr 增加了引用计数,没 ...
- php session的操作
[设置session数据] <?php session_start(); //初始化 //session文件中可以保存 dobule,integer,bool,array,object $_SE ...