通过代理为对象添加功能
2007-08-27 11:55
260 查看
using System;
using System.Threading;
using System.Runtime.Remoting.Proxies;
using System.Runtime.Remoting.Messaging;
using System.Runtime.Remoting;
namespace ConsoleApplication1
{
public interface ICalculator
{
double Add(double x, double y);
double Multiply(double x, double y);
}
public class PriortyProxy:RealProxy//定义增加优先级的代理类
{
readonly MarshalByRefObject target;
readonly ThreadPriority level;
public PriortyProxy(MarshalByRefObject target, Type type, ThreadPriority level):base(type)
{
this.target = target;
this.level = level;
}
public override IMessage Invoke(IMessage request)
{
IMethodCallMessage call=(IMethodCallMessage)request;
//调整优先级
Thread here = Thread.CurrentThread;
ThreadPriority old = here.Priority;
here.Priority = level;
//转发调用
IMessage response = RemotingServices.ExecuteMessage(target, call);
//回复为旧的优先级
here.Priority = old;
// 将响应消息返回给透明代理
return response;
}
}
public class MyCalc : MarshalByRefObject, ICalculator
{
public static MyCalc Create(ThreadPriority level)//创建插入代理的工厂方法
{
MyCalc target = new MyCalc();
PriortyProxy rp = new PriortyProxy(target, typeof(MyCalc), level);
return (MyCalc)rp.GetTransparentProxy();
}
private MyCalc() { }
public double Add(double x, double y) { return x + y; }
public double Multiply(double x, double y) { return x * y; }
}
class Program
{
static void Main(string[] args)
{
MyCalc c = MyCalc.Create(ThreadPriority.Highest);
Console.WriteLine(c.Add(1.0, 3.34));
Console.WriteLine(c.Multiply(2.0, 3.14));
Console.ReadLine();
}
}
}
上述例子为MyCalc类的方法调用实现线程优先级的调整。将优先级调整代码从MyCalc类中分离了出来,但是这并不是透明的。MyCalc必须些一个工厂方法!也必须使用该方法。不能直接用new来创建MyCalc!
上面的例子来自<<.net本质论>>第七章!
using System.Threading;
using System.Runtime.Remoting.Proxies;
using System.Runtime.Remoting.Messaging;
using System.Runtime.Remoting;
namespace ConsoleApplication1
{
public interface ICalculator
{
double Add(double x, double y);
double Multiply(double x, double y);
}
public class PriortyProxy:RealProxy//定义增加优先级的代理类
{
readonly MarshalByRefObject target;
readonly ThreadPriority level;
public PriortyProxy(MarshalByRefObject target, Type type, ThreadPriority level):base(type)
{
this.target = target;
this.level = level;
}
public override IMessage Invoke(IMessage request)
{
IMethodCallMessage call=(IMethodCallMessage)request;
//调整优先级
Thread here = Thread.CurrentThread;
ThreadPriority old = here.Priority;
here.Priority = level;
//转发调用
IMessage response = RemotingServices.ExecuteMessage(target, call);
//回复为旧的优先级
here.Priority = old;
// 将响应消息返回给透明代理
return response;
}
}
public class MyCalc : MarshalByRefObject, ICalculator
{
public static MyCalc Create(ThreadPriority level)//创建插入代理的工厂方法
{
MyCalc target = new MyCalc();
PriortyProxy rp = new PriortyProxy(target, typeof(MyCalc), level);
return (MyCalc)rp.GetTransparentProxy();
}
private MyCalc() { }
public double Add(double x, double y) { return x + y; }
public double Multiply(double x, double y) { return x * y; }
}
class Program
{
static void Main(string[] args)
{
MyCalc c = MyCalc.Create(ThreadPriority.Highest);
Console.WriteLine(c.Add(1.0, 3.34));
Console.WriteLine(c.Multiply(2.0, 3.14));
Console.ReadLine();
}
}
}
上述例子为MyCalc类的方法调用实现线程优先级的调整。将优先级调整代码从MyCalc类中分离了出来,但是这并不是透明的。MyCalc必须些一个工厂方法!也必须使用该方法。不能直接用new来创建MyCalc!
上面的例子来自<<.net本质论>>第七章!
相关文章推荐
- decorate -- 通过反复包含对像,并对已包含对象方法添加功能
- 通过Spring Aop编程思想,利用注解为某些对象添加新的功能
- 黑马程序员--09.动态与代理AOP--06【动态代理实例化的过程升级--目标对象+系统功能的参数化】【实现类似Spring的可配置AOP框架】
- 完成一个学生管理程序,使用学号作为键添加5个学生对象,并可以将全部信息保存在文件中,可以实现对学生信息的学号查找,输出全部学生信息的功能。
- 代理模式 -- 通过代理对象间接访问对象
- Spring进阶之路(10)-Advice简单介绍以及通过cglib生成AOP代理对象
- [转]让用户通过宏和插件向您的 .NET 应用程序添加功能
- Spring进阶之路(10)-Advice简介以及通过cglib生成AOP代理对象
- jTopo通过给node添加属性以达到只传参数而不显示的功能
- 通过字符串添加对应对象
- Android中可通过二维码扫描,直接显示个人名片信息,而且可以点击添加,调用系统的添加联系人功能
- 通过反射创建动态代理对象(二)
- python 面向对象之添加功能
- 通过 Parameter 对象添加 Ajax 请求时的参数
- QTP在录制脚本的时候会将鼠标点击过的对象自动添加到对象库中,而那些未被点击过的对象将不会被添加,导致针对某一对象手动添加的脚本运行不通过,因此需要手动把测试对象添加到对象库中,方法如下:
- 在RFT中如何通过脚本获取已添加到某个脚本中的测试对象?
- spring通过代理工厂创建代理对象
- 简单演示对象间通过BLOCK回调修改属性的功能
- 继承与合成基本概念 继承:可以基于已经存在的类构造一个新类。继承已经存在的类就可以复用这些类的方法和域。在此基础上,可以添加新的方法和域,从而扩充了类的功能。 合成:在新类里创建原有的对象称为合成。
- K8S 集群二进制安装(未完,代理功能与前端UI未验证通过)