动态代理的原理
原理其实很简单,就是在运行时生成新的对象,姑且叫做T,并使T继承自需要代理的原对象,调用过程实际是调用了新的对象T.
通过对T中方法或属性等,添加些自定义的操作,从而实现对原对象访问的封装.
动态代理实现(利用castle)
castle的动态代理需要下面几步
- 自定义一个拦截器,必须实现 IInterceptor 接口
- 使用 ProxyGenerator 对象创建代理对象,对象中包含很多方法
- 对原对象的所有操作,都使用代理对象代替
- 在拦截器的方法中,加入自定义的操作,比如 记录参数调用日志,异常记录等.
简单拦截器实现代码: SampleInterceptor.cs
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Castle.Core.Interceptor;
using System.Reflection;
/// <summary>
/// 拦截器示例
/// </summary>
public class SampleInterceptor : IInterceptor
{
public SampleInterceptor()
{
//
//TODO: 在此处添加构造函数逻辑
//
}
public void Intercept(IInvocation invocation)
{
output("开始进入拦截器");
MethodInfo concreteMethod = invocation.GetConcreteMethod();
if (!invocation.MethodInvocationTarget.IsAbstract)
{
output("开始执行 " + concreteMethod.Name);
//执行原对象中的方法
invocation.Proceed();
output("执行结果 " + invocation.ReturnValue);
}
output("执行完毕");
}
private void output(string Message)
{
HttpContext.Current.Response.Write(Message + "<br>");
}
}
示例中使用的接口 : IPerson.cs
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
/// <summary>
///IPerson 的摘要说明
/// </summary>
public interface IPerson
{
/// <summary>
/// 姓名
/// </summary>
string Name { get; }
/// <summary>
/// 地址
/// </summary>
string Address { get; }
/// <summary>
/// 正在做什么
/// </summary>
/// <returns></returns>
string Doing();
}
对接口的实现:Person.cs
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
/// <summary>
///Person 的摘要说明
/// </summary>
public class Person : IPerson
{
public Person()
{
//
//TODO: 在此处添加构造函数逻辑
//
}
#region IPerson 成员
public string Name
{
get { return "我是花生米"; }
}
public string Address
{
get { return "我住在 http://pignut-wang.iteye.com/ "; }
}
public string Doing()
{
return "我正在写blog";
}
#endregion
}
所有要使用到的对象都准备好了,下面就是调用的代码
using System;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Castle.DynamicProxy;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//创建拦截器对象
SampleInterceptor Interceptor = new SampleInterceptor();
//给person类生成代理
ProxyGenerator Generator = new ProxyGenerator();
IPerson p = Generator.CreateInterfaceProxyWithTarget<IPerson>(new Person(), Interceptor);
//执行方法看效果
p.Doing();
}
}
执行的效果就是在页面上输出4句话,如下
开始进入拦截器
开始执行 Doing
执行结果 我正在写blog
执行完毕
动态代理的应用
这个我就不罗唆说了,随便搜一下,大把的,只说一句.
主要用于和业务无关几乎每个方法都要用到的操作,如权限验证,日志记录,自动事务控制....反正很多了.
分享到:
相关推荐
aop castle ioc 动态代理 webservice
Castle Dynamic Proxy代理使用
添加了跟踪模型类中的更改的功能; 使用代理拦截器执行附加到模型属性的规则
使用Nginx作为代理服务器,HTTP的接口代理没有问题,HTPPS的接口代理Java后台总是报错,主要是因为项目使用的JDK1.6,不支持TLS1.2导致握手失败。通过BouncyCastle的第三方jar包可以解决这个问题。也可以使用Nginx的...
模仿vue 实现的 在winform 上实现的数据模型双向绑定。升级版使用 castle 进行动态代理,较与旧版节省大量代码的书写,同时将核心抽出来,还能用到其它需要监听属性变化的地方
它还具有Castle DynamicProxy(轻量级运行时代理生成器)和Castle DictionaryAdapter 。 请参阅。 发行版 请参阅。 自版本4.1.0起,调试符号可在AppVeyor构建工件的符号包中使用。 例如, 。 执照 Castle Core是...
使用了Castle.ActiveRecord +Spring.NET+ log4net+CSV+NHibernate+ SmartThreadPool+SQLite.NET. 还有动态代理切换、易思验证码等技术。
代理服务器。 P H O E N I X 4.0 What is it? ----------- It is meta-server - a server kernel on top of which other servers are built. Where is it? ------------ ...
动态Linq查询生成器 dynamic-linq-query-builder是一个小型库,允许任何.Net框架类集合在运行时动态过滤。...dynamic-linq-query-builder可以通过nuget UI(作为Castle.DynamicLinqQueryBuilder)或nuget软
使用了Castle.ActiveRecord +Spring.NET+ log4net+CSV+NHibernate+ SmartThreadPool+SQLite.NET. 还有动态代理切换、易思验证码等技术。 菜鸟们可以参考。有问题请联系我上海Q-362,505,707。
使用了Castle.ActiveRecord +Spring.NET+ log4net+CSV+NHibernate+ SmartThreadPool+SQLite.NET. 还有动态代理切换、易思验证码等技术。 菜鸟们可以参考。有问题请联系我上海Q-362,505,707。
使用了Castle.ActiveRecord +Spring.NET+ log4net+CSV+NHibernate+ SmartThreadPool+SQLite.NET. 还有动态代理切换、易思验证码等技术。 菜鸟们可以参考。有问题请联系我上海Q-362,505,707。
使用了Castle.ActiveRecord +Spring.NET+ log4net+CSV+NHibernate+ SmartThreadPool+SQLite.NET. 还有动态代理切换、易思验证码等技术。 菜鸟们可以参考。有问题请联系我上海Q-362,505,707。
使用了Castle.ActiveRecord +Spring.NET+ log4net+CSV+NHibernate+ SmartThreadPool+SQLite.NET. 还有动态代理切换、易思验证码等技术。 菜鸟们可以参考。有问题请联系我上海Q-362,505,707。
使用了Castle.ActiveRecord +Spring.NET+ log4net+CSV+NHibernate+ SmartThreadPool+SQLite.NET. 还有动态代理切换、易思验证码等技术。 菜鸟们可以参考。有问题请联系我上海Q-362,505,707。
贫民窟解锁棒棒糖信托代理样本。 详情请看以下帖子:如何构建复制到 AOSP 树中的... 在libs/删除 Spongy Castle 轻量级 API jar 并相应地调整Android.mk (使用测试sc-light-jdk15on-1.47.0.2.jar )。 运行mm来构建。
为Javascript客户端创建REST-API 创建SOAP Web服务与.NET以外的其他平台一起使用创建需要在多个群集节点上运行的服务器应用程序事实与特征在运行时为远程服务创建代理对象(在后台使用Castle.DynamicProxy) 服务...
ASP.NET Boilerplate采用了以下技术: 服务器端: ASP.NET MVC 5、Web API 2...自动创建Javascript 的代理层来更方便使用Web Api 封装一些Javascript 函数,更方便地使用ajax、消息框、通知组件、忙状态的遮罩层等等
本文主要介绍了nginx 多个location转发任意请求或访问静态资源文件的实现,分享给大家,具体如下:... #使用通配符只有在没有匹配上其他location的情况下会进入 location / { #去掉了只有url才转发的限制,因为直接访