C# 动态代理集合接口

C# 动态代理集合接口,c#,proxy,adapter,dynamicobject,C#,Proxy,Adapter,Dynamicobject,我偶尔有理由编写集合类适配器,即为实现IList代理其方法的类创建适配器,同时添加一些额外的功能。IList接口有许多方法/属性,我想知道直通代理方法是否可以动态实现?我查看了DynamicObject,但只能找到几个将数据代理到类的简单示例,即代理一个只有属性的类 是否可以代理IList e、 g 公共类ListProxy:IList { 私人IList_adaptee; 公共列表代理(IList适配器) { _被改编者 } //动态实现直通式IList方法/属性 } 像这样的东西 using

我偶尔有理由编写集合类适配器,即为实现
IList
代理其方法的类创建适配器,同时添加一些额外的功能。IList接口有许多方法/属性,我想知道直通代理方法是否可以动态实现?我查看了
DynamicObject
,但只能找到几个将数据代理到类的简单示例,即代理一个只有属性的类

是否可以代理
IList

e、 g

公共类ListProxy:IList
{
私人IList_adaptee;
公共列表代理(IList适配器)
{
_被改编者
}
//动态实现直通式IList方法/属性
}
像这样的东西

using System;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.Remoting.Messaging;
using System.Runtime.Remoting.Proxies;

class Program
{
    static void Main(string[] args)
    {
        IList<string> listProxy = MyProxyGenerator.Create<IList<string>>(new ListProxy<string>(new List<string>() { "aa","bb" }));
        bool b1 = listProxy.Contains("aa");
        bool b2 = listProxy.Contains("cc");
        int count = listProxy.Count;
        string s = listProxy[1];
    }

    public class ListProxy<T>
    {
        private IList<T> _adaptee;

        //Only method needed by proxy generator
        object Adaptee
        {
            get { return _adaptee; }
        }

        public ListProxy(IList<T> adaptee)
        {
            _adaptee = adaptee;
        }
    }

    class MyProxyGenerator : RealProxy
    {
        Type _Type;
        object _Instance;

        public static T Create<T>(object instance)
        {
            return (T)new MyProxyGenerator(typeof(T),instance).GetTransparentProxy();
        }

        MyProxyGenerator(Type type,object instance) : base(type)
        {
            _Type = type;
            _Instance = instance.GetType().InvokeMember("get_Adaptee", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.InvokeMethod, null, instance, null);
        }

        public override IMessage Invoke(IMessage msg)
        {
            IMethodCallMessage methodMessage = new MethodCallMessageWrapper((IMethodCallMessage)msg);
            string method = (string)msg.Properties["__MethodName"];
            object[] args = (object[])msg.Properties["__Args"];

            object retObj = _Instance.GetType().InvokeMember(method, BindingFlags.Public | BindingFlags.Instance | BindingFlags.InvokeMethod,null,_Instance,args);
            return new ReturnMessage(retObj,methodMessage.Args,methodMessage.ArgCount,methodMessage.LogicalCallContext,methodMessage);
        }
    }


}
使用系统;
使用System.Collections.Generic;
运用系统反思;
使用System.Runtime.Remoting.Messaging;
使用System.Runtime.Remoting.Proxies;
班级计划
{
静态void Main(字符串[]参数)
{
IList listProxy=MyProxyGenerator.Create(新建listProxy(new List(){“aa”,“bb”}));
bool b1=listProxy.Contains(“aa”);
bool b2=listProxy.Contains(“cc”);
int count=listProxy.count;
字符串s=listProxy[1];
}
公共类ListProxy
{
私人IList_adaptee;
//代理生成器所需的唯一方法
对象适配器
{
获取{return\u adaptee;}
}
公共列表代理(IList适配器)
{
_被改编者=被改编者;
}
}
类MyProxyGenerator:RealProxy
{
类型_类型;
对象_实例;
公共静态T创建(对象实例)
{
返回(T)新的MyProxyGenerator(typeof(T),instance).GetTransparentProxy();
}
MyProxyGenerator(类型,对象实例):基(类型)
{
_类型=类型;
_Instance=Instance.GetType().InvokeMember(“get_Adaptee”,BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.InvokeMethod,null,Instance,null);
}
公共覆盖IMessage调用(IMessage msg)
{
IMethodCallMessage methodMessage=新方法CallMessageWrapper((IMethodCallMessage)消息);
string方法=(string)msg.Properties[“_方法名”];
object[]args=(object[])msg.Properties[“_args”];
object retObj=_Instance.GetType().InvokeMember(方法,BindingFlags.Public | BindingFlags.Instance | BindingFlags.InvokeMethod,null,_实例,args);
返回新的ReturnMessage(retObj、methodMessage.Args、methodMessage.ArgCount、methodMessage.LogicalCallContext、methodMessage);
}
}
}
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.Remoting.Messaging;
using System.Runtime.Remoting.Proxies;

class Program
{
    static void Main(string[] args)
    {
        IList<string> listProxy = MyProxyGenerator.Create<IList<string>>(new ListProxy<string>(new List<string>() { "aa","bb" }));
        bool b1 = listProxy.Contains("aa");
        bool b2 = listProxy.Contains("cc");
        int count = listProxy.Count;
        string s = listProxy[1];
    }

    public class ListProxy<T>
    {
        private IList<T> _adaptee;

        //Only method needed by proxy generator
        object Adaptee
        {
            get { return _adaptee; }
        }

        public ListProxy(IList<T> adaptee)
        {
            _adaptee = adaptee;
        }
    }

    class MyProxyGenerator : RealProxy
    {
        Type _Type;
        object _Instance;

        public static T Create<T>(object instance)
        {
            return (T)new MyProxyGenerator(typeof(T),instance).GetTransparentProxy();
        }

        MyProxyGenerator(Type type,object instance) : base(type)
        {
            _Type = type;
            _Instance = instance.GetType().InvokeMember("get_Adaptee", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.InvokeMethod, null, instance, null);
        }

        public override IMessage Invoke(IMessage msg)
        {
            IMethodCallMessage methodMessage = new MethodCallMessageWrapper((IMethodCallMessage)msg);
            string method = (string)msg.Properties["__MethodName"];
            object[] args = (object[])msg.Properties["__Args"];

            object retObj = _Instance.GetType().InvokeMember(method, BindingFlags.Public | BindingFlags.Instance | BindingFlags.InvokeMethod,null,_Instance,args);
            return new ReturnMessage(retObj,methodMessage.Args,methodMessage.ArgCount,methodMessage.LogicalCallContext,methodMessage);
        }
    }


}