.net Aop 实现原理
本文实现所有继承BaseModel的类都通过代理拦截
using System;
using System.Reflection;
using System.Collections.Generic;
using System.Runtime.Remoting.Proxies;
using System.Runtime.Remoting.Messaging;
using System.Runtime.Remoting.Activation; namespace Soffice.DevelopFramework.Common.DataAccess
{
[AopAttribute]
public class BaseModel : ContextBoundObject
{
private Dictionary<string, object> Fields = new Dictionary<string, object>(); public void AddField(string fieldName, object value)
{
Fields.Add(fieldName, value);
} public Dictionary<string, object> GetFields()
{
return Fields;
}
} class AopAttribute : ProxyAttribute
{
public override MarshalByRefObject CreateInstance(Type serverType)
{
AopProxy realProxy = new AopProxy(serverType);
return realProxy.GetTransparentProxy() as MarshalByRefObject;
}
} class AopProxy : RealProxy
{
MethodInfo method = null;
public AopProxy(Type serverType)
: base(serverType)
{
method = serverType.GetMethod("AddField");
}
public override IMessage Invoke(IMessage msg)
{
//消息拦截之后,就会执行这里的方法。
if (msg is IConstructionCallMessage) // 如果是构造函数,按原来的方式返回即可。
{
IConstructionCallMessage constructCallMsg = msg as IConstructionCallMessage;
IConstructionReturnMessage constructionReturnMessage = this.InitializeServerObject((IConstructionCallMessage)msg);
RealProxy.SetStubData(this, constructionReturnMessage.ReturnValue);
return constructionReturnMessage;
}
else if (msg is IMethodCallMessage) //如果是方法调用(属性也是方法调用的一种)
{
IMethodCallMessage callMsg = msg as IMethodCallMessage;
object[] args = callMsg.Args;
IMessage message;
try
{
if (callMsg.MethodName.StartsWith("set_") && args.Length == )
{
//这里检测到是set方法,然后应怎么调用对象的其它方法呢?
method.Invoke(GetUnwrappedServer(), new object[] { callMsg.MethodName.Substring(), args[] });//对属性进行调用
}
object o = callMsg.MethodBase.Invoke(GetUnwrappedServer(), args);
message = new ReturnMessage(o, args, args.Length, callMsg.LogicalCallContext, callMsg);
}
catch (Exception e)
{
message = new ReturnMessage(e, callMsg);
}
return message;
}
return msg;
}
}
}
.net Aop 实现原理的更多相关文章
- 【Spring】Spring AOP实现原理
		Spring AOP实现原理 在之前的一文中介绍过Spring AOP的功能使用,但是没有深究AOP的实现原理,今天正好看到几篇好文,于是就自己整理了一下AOP实现的几种方式,同时把代理模式相关知识也 ... 
- Spring AOP 实现原理
		什么是AOP AOP(Aspect-OrientedProgramming,面向方面编程),可以说是OOP(Object-Oriented Programing,面向对象编程)的补充和完善.OOP引入 ... 
- Spring核心框架 - AOP的原理及源码解析
		一.AOP的体系结构 如下图所示:(引自AOP联盟) 层次3语言和开发环境:基础是指待增加对象或者目标对象:切面通常包括对于基础的增加应用:配置是指AOP体系中提供的配置环境或者编织配置,通过该配置A ... 
- 何为代理?jdk动态代理与cglib代理、spring Aop代理原理浅析
		原创声明:本博客来源为本人原创作品,绝非他处摘取,转摘请联系博主 代理(proxy)的定义:为某对象提供代理服务,拥有操作代理对象的功能,在某些情况下,当客户不想或者不能直接引用另一个对象,而代理对象 ... 
- 循序渐进之Spring AOP(1) - 原理
		AOP全称是Aspect Oriented Programing,通常译为面向切面编程.利用AOP可以对面向对象编程做很好的补充. 用生活中的改装车比喻,工厂用面向对象的方法制造好汽车后,车主往往有些 ... 
- Spring Aop底层原理详解
		Spring Aop底层原理详解(来源于csdn:https://blog.csdn.net/baomw) 
- 深入浅析Spring的AOP实现原理
		转载来源:https://www.jb51.net/article/81788.htm AOP(Aspect-OrientedProgramming,面向切面编程),可以说是OOP(Object-Or ... 
- spring AOP底层原理实现——jdk动态代理
		spring AOP底层原理实现——jdk动态代理 
- Spring AOP底层原理
		------------------siwuxie095 Spring AOP 底层原理 AOP 即 Aspect Or ... 
- jdk动态代理与cglib代理、spring Aop代理原理-代理使用浅析
		原创声明:本博客来源为本人原创作品,绝非他处摘取,转摘请联系博主 代理(proxy)的定义:为某对象提供代理服务,拥有操作代理对象的功能,在某些情况下,当客户不想或者不能直接引用另一个对象,而代理对象 ... 
随机推荐
- VMware网络适配器
			VMware 提供的网络连接有 5 种,分别是"桥接模式"."NAT 模式"."仅主机模式"."自定义"和"L ... 
- python 使用夜神模拟器
			安装版本为6.2.8.0 1.模拟器安装证书 打开模拟器,点击浏览器 在浏览器里输入:mitm.it 出现如下: 选择安卓进行安装 比如:sks123 2.设置代理 输入密码:sks123 上面刚才设 ... 
- dubbo源码分析之基于SPI的强大扩展
			https://blog.csdn.net/luoyang_java/article/details/86609045 Dubbo采用微内核+插件体系,使得设计优雅,扩展性强.那所谓的微内核+插件体系 ... 
- Oracle 行转列 动态出转换的列
			本文链接:https://blog.csdn.net/Huay_Li/article/details/82924443 10月的第二天,前天写了个Oracle中行转列的pivot的基本使用方法,然后, ... 
- OpenCV3与深度学习实例:Dlib+VGG Face实现两张脸部图像相似度比较
			原文:https://my.oschina.net/wujux/blog/2221444 实现思路: 1.使用Dlib识别并提取脸部图像 2.使用VGG Face模型提取脸部特征 3.使用余弦相似度算 ... 
- WSL记录
			cmder(mini版)作为wsl的终端,很好用,可以split屏.但是:千万不要在settings里面设置start up(启动) 里面设置 命令行“bash -cur_console:p1”!目前 ... 
- xpath和contains模糊匹配
			xpath可以以标签定位,也可以@任意属性: 如:以input标签定位:driver.find_element_by_xpath("//input[@id='kw']") 如:@t ... 
- 为什么0x00400000是可执行文件的默认基址?EXE base address start with 400000H,Why is 0x00400000 the default base address for an executable?
			DLL的默认基址是0x10000000,但EXE的默认基址是0x00400000.为什么EXE特别值?4 兆字节有什么特别之处它与x86上单页目录条目映射的地址空间量和1987年的设计决策有关.对EX ... 
- Gerrit - 初始配置
			1 - 插件管理 1.1 下载并安装插件 以reviewers插件为例. 在GerritForge(https://gerrit-ci.gerritforge.com/),找到对应gerrit 版本的 ... 
- win7下IntelliJ IDEA使用curl
			curl是利用URL语法在命令行方式下工作的开源文件传输工具 curl命令可以在开发web应用时,模拟前端发起的HTTP请求 1.下载curl https://curl.haxx.se/downloa ... 
