使用.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. invalid location of tag 解决办法

    转自:https://blog.csdn.net/tanzuai/article/details/41896579 在jsp页面使用标签过程中有时候不注意规则的话,eclipse会提示一些错误,下面针 ...

  2. js 数组拷贝与深拷贝

    1.对于普通数组(数组元素为数字或者字符串) var _testCopy = [1,2,3].concat();//拷贝数组(浅拷贝) 2.对于对象数组 (深拷贝) //形如var _objArr=[ ...

  3. Java同步注解:@ThreadSafe、@Immutable、@NotThreadSafe、@GuardedBy

    Java并发编程中,用到了一些专门为并发编程准备的 Annotation.主要包括三类: <dependency> <groupId>net.jcip</groupId& ...

  4. nativefier - 快速把任意网页生成桌面应用程序

    使用前端技术开发桌面应用的技术已经相当成熟了,像早先的 NW.js,如今很火的 Electron 等,都可以轻松实现.今天给大家分享的 nativefier 就是基于 Electron 封装的,可以帮 ...

  5. None.js 第一步 开启一个服务 hello world

    引入 http 模块 var http = require('http'); 创建服务器 http.createServer(function (request, response) { // 发送一 ...

  6. gai_strerror函数

    一.函数原型 #include <netdb.h> const char *gai_strerror(int error); 返回:指向错误描述消息字符串的指针 二.由getaddrinf ...

  7. Rsync + inotify 实现文件实时同步

    Rsync 用来实现触发式的文件同步. Inotify-tools是一套组件,Linux内核从2.6.13版本开始提供了inotify通知接口,用来监控文件系统的各种变化情况,如文件存取.删除.移动等 ...

  8. js开发模式

    js中的开发模式进化史: js中有最初的只能由基本数据类型描述——>单例模式-->工厂模式-->构造函数模式-->原型模式-->各个模式相结合的混合模式,下面我会给大家逐 ...

  9. python3:实现输出等边三角形、直角三角形

    学习python,用的是3.5的版本: 记录一下学习历程~ 一.先来一个简单的,输出直角三角形: ***知识点:for循环,range()函数 二.进阶:输出等边三角形 ******知识点:嵌套for ...

  10. 数组去重的4种方法(Which one is the fastest???嘻嘻嘻....)

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...