使用.Net Remoting/RealProxy

采用TransparentProxy和RealProxy实现对象的代理,实现思路如下:Client -TransparentProxy - RealProxy - Target Object

下面实现自定义的TransparentProxy和RealProxy

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Remoting.Messaging;
using System.Runtime.Remoting.Proxies;
using System.Text;
using System.Threading.Tasks; namespace MyRealProxyApplication
{
public class MyRealProxy<T> : RealProxy
{
private T _target;
public MyRealProxy(T target)
: base(typeof(T))
{
this._target = target;
}
public override IMessage Invoke(IMessage msg)
{
PreProceede(msg);
IMethodCallMessage callMessage = (IMethodCallMessage)msg;
object returnValue = callMessage.MethodBase.Invoke(this._target, callMessage.Args);
PostProceede(msg);
return new ReturnMessage(returnValue, new object[], , null, callMessage);
}
public void PreProceede(IMessage msg)
{
Console.WriteLine("方法执行前");
}
public void PostProceede(IMessage msg)
{
Console.WriteLine("方法执行后");
}
} }
using System;

namespace MyRealProxyApplication
{
public static class TransparentProxy
{
public static T Create<T>()
{
T instance = Activator.CreateInstance<T>();
MyRealProxy<T> realProxy = new MyRealProxy<T>(instance);
T transparentProxy = (T)realProxy.GetTransparentProxy();
return transparentProxy;
}
}
}

用户注册接口和实现

 public interface IUserProcessor
{
void RegUser(User user);
} public void RegUser(User user)
{
Console.WriteLine("用户已注册。");
} public class User
{
public string Name{get;set;}
public string PassWord{get;set;} }

客户端调用

public class Transfer
{
public static void Run()
{
try
{
User user = new User() { Name = "lee", PassWord = "" };
UserProcessor userprocessor = TransparentProxy.Create<UserProcessor>();
userprocessor.RegUser(user);
}
catch (Exception ex)
{
throw ex;
}
}
}

(二)远程代理Remoting/RealProxy的更多相关文章

  1. 设计模式之代理模式(Proxy Pattern)_远程代理解析

    一.什么是代理模式? 顾名思义,代理就是第三方,比如明星的经纪人,明星的事务都交给经纪人来处理,明星只要告诉经纪人去做什么,经纪人自然会想办法去做,做完之后再把结果告诉明星就好了 本来是调用者与被调用 ...

  2. rmi 与 远程代理复习

    ref:http://blog.csdn.net/pipisky2006/article/details/7296592 RMI: 远程方法调用,简单来说以前是调用本地对象的方法,现在如果对象在另外一 ...

  3. 远程代理模式-Remote Proxy(Java实现)

    远程代理模式-Remote Proxy 服务端通过rmi将对象注册到远程服务, 客户端使用时, 只需要通过rmi协议获取即可, 只要接口统一, 即可不需要知道内部具体实现, 直接调用使用. Compa ...

  4. JavaScript:动态代理之远程代理

    背景 2008 第一次使用 AJAX 的时候好像使用的是 AJAX.NET,该组件支持为服务器 C# 类型提供 JS 代理,当时不是十分的明白.设计模式中有代理模式,不过真正需要我们手工写代理的次数却 ...

  5. Java 设计模式系列(十二)代理模式

    Java 设计模式系列(十二)代理模式 代理模式是对象的结构模式.代理模式给某一个对象提供一个代理对象,并由代理对象控制对原对象的引用. package com.github.binarylei.de ...

  6. MITMProxy如何配置二次代理

    MITMProxy如何配置二次代理 0.2172018.09.05 11:13:15字数 232阅读 2609 前序: mitmproxy真的很强大,或许是大家都各自使用,或者没有相关的需求,导致我废 ...

  7. Httpoxy远程代理感染漏洞 [转]

    Httpoxy是一个最新曝出的一个CGI程序漏洞,它主要可能威胁到运行在CGI上的PHP,Go,Python和其他代码程序语言. 0×01  什么是httpoxy? 具体来说, httpoxy是一组影 ...

  8. git 学习使用总结二(远程仓库操作)

    这篇文章仅供自己以后翻阅加深记忆,要系统的学习 git 教程(中文版),请移步到 liaoxuefeng.com 学习 git 教程部分. 我使用的是 windows 系统,所以使用 Git Bash ...

  9. Linux学习之CentOS(二)----远程登录管理工具SecureCRT的使用

    [声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/3 ...

随机推荐

  1. java-solr solrj的使用

    新建一个maven项目,引入依赖: <dependencies> <dependency> <groupId>org.apache.solr</groupId ...

  2. VS中ipch文件夹和sdf文件的处理方式

    ipch文件夹和sdf是VS产生的预编译头文件和智能提示信息,对编码没有影响,可存放在固定的位置,定期进行清理

  3. 第28月第10天 iOS动态库

    1. https://www.cnblogs.com/wfwenchao/p/5577789.html https://github.com/wangzz/Demo http://www.kimbs. ...

  4. byte数组和int之间相互转化的方法

    Java中byte数组和int类型的转换,在网络编程中这个算法是最基本的算法,我们都知道,在socket传输中,发送者接收的数据都是byte数组,但是int类型是4个byte组成的,如何把一个整形in ...

  5. LOJ #2183「SDOI2015」序列统计

    有好多好玩的知识点 LOJ 题意:在集合中选$ n$个元素(可重复选)使得乘积模$ m$为$ x$,求方案数对$ 1004535809$取模 $ n<=10^9,m<=8000且是质数,集 ...

  6. __call__

    object.__call__(self[, args...]) Called when the instance is “called” as a function; if this method ...

  7. printf是在libc库中么?

    libc中果然有很多的函数,使用nm看了一下,里面竟然还有reboot函数,汗! 使用grep,可以看到各种 printf 也都在这里头. objdump是看函数的地址与函数名对应的,虽然也能证明pr ...

  8. F - Auxiliary Set HDU - 5927 (dfs判断lca)

    题目链接: F - Auxiliary Set HDU - 5927 学习网址:https://blog.csdn.net/yiqzq/article/details/81952369题目大意一棵节点 ...

  9. zabbix系列 ~ linux监控相关

    Linux 监控 一 相关名词解释    cpu context switch (上下文切换)   1 定义 CPU给每个任务一定的服务时间,当时间片轮转的时候,需要把当前状态保存下来,同时加载下一个 ...

  10. setInterval 传值设参数

    <script type="text/javascript" > window.onload=function(){ for(var i=1;i<3;i++){ ...