关于代理类的使用情景和基本代码
2009-10-22 10:06
453 查看
当你做好一个项目的时候,你突然发现运行起来比较慢,各种异常又没有写进日志里面,这种想做一些监测,比如看某个类每个方法执行时间,把异常进行管理,但没有人愿意把每个方法都写上监测逻辑,那这时代理就有了使用场景,主要是只要修改不多的程序,就可以让某类在方法或属性被调用时,先访问某个方法.具体如下:
代码:
被代理的类
public class Class2 : MarshalByRefObject
{
private Class2()
{
}
public static Class2 CreateInstance()
{
Class2 mobj = new Class2();
proxytem realProxy = new proxytem(typeof(Class2),mobj);
Class2 refObj = (Class2)realProxy.GetTransparentProxy();
return refObj;
}
public int sum()
{
return 9;
}
}
proxytem 类
public class proxytem : RealProxy
{
MarshalByRefObject myMarshalByRefObject;
public proxytem(Type myType, MarshalByRefObject mobj)
: base(myType)
{
myMarshalByRefObject = mobj;
}
public override System.Runtime.Remoting.Messaging.IMessage Invoke(System.Runtime.Remoting.Messaging.IMessage message)
{
System.Runtime.Remoting.Messaging.IMessage retMsg = null;
if (message is IConstructionCallMessage)
{
IConstructionCallMessage ccm = (IConstructionCallMessage)message;
RemotingServices.GetRealProxy(myMarshalByRefObject).InitializeServerObject(ccm);
ObjRef objRef = RemotingServices.Marshal(myMarshalByRefObject);
RemotingServices.Unmarshal(objRef);
retMsg = EnterpriseServicesHelper.CreateConstructionReturnMessage(ccm, (MarshalByRefObject)this.GetTransparentProxy());
}
else if (message is IMethodCallMessage)
{
IMethodCallMessage mcm = (IMethodCallMessage)message;
DateTime start = DateTime.Now;
//执行方法调用
retMsg = RemotingServices.ExecuteMessage(myMarshalByRefObject, mcm);
IMethodReturnMessage mrm = (IMethodReturnMessage)retMsg;
DateTime end = DateTime.Now;
//有了start和end就可以统计时间
Exception ex = mrm.Exception;
//如果没有异常,就进行序列化测试,SerializeHelper这个类可以检测对象是否可序列化,具体百度一下
//if (ex == null)
//{
// try
// {
// SerializeHelper.DataContractSerializer(mrm.ReturnValue);
// }
// catch (Exception e)
// {
// ex = e;
// }
//}
ObjRef objRef = RemotingServices.Marshal(myMarshalByRefObject);//获得原来的类的实例
MethodInfo mi = mcm.MethodBase.DeclaringType.GetMethod(mcm.MethodName);//获得方法信息
Activator.CreateInstance(mi.ReturnType);//可以获得方法反回类型的实例,这就保证方法不会报错,一定会返回一个new出来的实例
object a= ((ReturnMessage)retMsg).ReturnValue;//可以得到返回值.
}
return retMsg;
}
}
调用:
Class2 c2 = Class2.CreateInstance();
int i =c2.sum();
1.被代理的类要继承MarshalByRefObject类,RealProxy是系统的一个基本代理类,new RealProxy(MarshalByRefObject)后,如果访问MarshalByRefObject实例的方法,就会调用RealProxy.Invoke的方法,利用这一点,找个类继承RealProxy,把实例也传进去,然后重写invoke,在invoke里面利用 retMsg = RemotingServices.ExecuteMessage(myMarshalByRefObject, mcm);
来执行方法,同时也可以做很多统计工作
代码:
被代理的类
public class Class2 : MarshalByRefObject
{
private Class2()
{
}
public static Class2 CreateInstance()
{
Class2 mobj = new Class2();
proxytem realProxy = new proxytem(typeof(Class2),mobj);
Class2 refObj = (Class2)realProxy.GetTransparentProxy();
return refObj;
}
public int sum()
{
return 9;
}
}
proxytem 类
public class proxytem : RealProxy
{
MarshalByRefObject myMarshalByRefObject;
public proxytem(Type myType, MarshalByRefObject mobj)
: base(myType)
{
myMarshalByRefObject = mobj;
}
public override System.Runtime.Remoting.Messaging.IMessage Invoke(System.Runtime.Remoting.Messaging.IMessage message)
{
System.Runtime.Remoting.Messaging.IMessage retMsg = null;
if (message is IConstructionCallMessage)
{
IConstructionCallMessage ccm = (IConstructionCallMessage)message;
RemotingServices.GetRealProxy(myMarshalByRefObject).InitializeServerObject(ccm);
ObjRef objRef = RemotingServices.Marshal(myMarshalByRefObject);
RemotingServices.Unmarshal(objRef);
retMsg = EnterpriseServicesHelper.CreateConstructionReturnMessage(ccm, (MarshalByRefObject)this.GetTransparentProxy());
}
else if (message is IMethodCallMessage)
{
IMethodCallMessage mcm = (IMethodCallMessage)message;
DateTime start = DateTime.Now;
//执行方法调用
retMsg = RemotingServices.ExecuteMessage(myMarshalByRefObject, mcm);
IMethodReturnMessage mrm = (IMethodReturnMessage)retMsg;
DateTime end = DateTime.Now;
//有了start和end就可以统计时间
Exception ex = mrm.Exception;
//如果没有异常,就进行序列化测试,SerializeHelper这个类可以检测对象是否可序列化,具体百度一下
//if (ex == null)
//{
// try
// {
// SerializeHelper.DataContractSerializer(mrm.ReturnValue);
// }
// catch (Exception e)
// {
// ex = e;
// }
//}
ObjRef objRef = RemotingServices.Marshal(myMarshalByRefObject);//获得原来的类的实例
MethodInfo mi = mcm.MethodBase.DeclaringType.GetMethod(mcm.MethodName);//获得方法信息
Activator.CreateInstance(mi.ReturnType);//可以获得方法反回类型的实例,这就保证方法不会报错,一定会返回一个new出来的实例
object a= ((ReturnMessage)retMsg).ReturnValue;//可以得到返回值.
}
return retMsg;
}
}
调用:
Class2 c2 = Class2.CreateInstance();
int i =c2.sum();
1.被代理的类要继承MarshalByRefObject类,RealProxy是系统的一个基本代理类,new RealProxy(MarshalByRefObject)后,如果访问MarshalByRefObject实例的方法,就会调用RealProxy.Invoke的方法,利用这一点,找个类继承RealProxy,把实例也传进去,然后重写invoke,在invoke里面利用 retMsg = RemotingServices.ExecuteMessage(myMarshalByRefObject, mcm);
来执行方法,同时也可以做很多统计工作
相关文章推荐
- 关于StoryBoard的使用(3)两个添加一个UINavigationController||UITabBarController最基本的使用
- Android关于TextureView理解及基本使用
- 关于如何使用eclipse开发工具写java代码
- android中基本文件输入输出流的使用(代码)
- Unity关于有些代码不能在除主线程外的线程使用的问题
- Git的使用二:基本配置及拉取代码
- Android中补间动画1----Animation的基本使用代码实现(平移,缩放,渐变,旋转)
- Linux下开发关于Samba/Vimrc/svn/tftp/等基本的配置使用
- 关于treeView1_AfterSelect方法中节点的使用(代码介绍)
- 关于OC和Swift混编的基本使用
- ARM启动代码及ADS基本使用
- 关于用TortoiseGit使用ssh地址下载项目代码
- Unity-关于Rigidbody如何使用代码冻结位置和旋转
- 03_MyBatis基本查询,mapper文件的定义,测试代码的编写,resultMap配置返回值,sql片段配置,select标签标签中的内容介绍,配置使用二级缓存,使用别名的数据类型,条件查询ma
- 关于“java的基本特征”详细代码 (全)
- Mybatis Generator Configuration Eclipse自动生成代码工具使用及实现基本crud
- 关于代码优化中inline函数的使用
- 关于Notification通知的基本使用
- 关于jquery的一些基本使用
- 关于ubuntu下,7z的基本使用(备忘)解压rar格式