Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从列表中创建动态代理<;T>;_C#_Castle_Castle Dynamicproxy_Dynamic Proxy - Fatal编程技术网

C# 如何从列表中创建动态代理<;T>;

C# 如何从列表中创建动态代理<;T>;,c#,castle,castle-dynamicproxy,dynamic-proxy,C#,Castle,Castle Dynamicproxy,Dynamic Proxy,我正在使用Castle动态代理CreateClassProxyWithTarget,使用的是已经存在的列表。基本上,我想拦截对列表索引器的调用 为了实现这个目标,我尝试了很多组合,但每次创建的代理都返回一个空列表 例如: using System; using System.Collections.Generic; using System.Linq; using System.Text; using Castle.DynamicProxy; namespace DP { class

我正在使用Castle动态代理CreateClassProxyWithTarget,使用的是已经存在的列表。基本上,我想拦截对列表索引器的调用

为了实现这个目标,我尝试了很多组合,但每次创建的代理都返回一个空列表

例如:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Castle.DynamicProxy;

namespace DP
{
    class Program
    {
        private static readonly ProxyGenerator _generator = new ProxyGenerator(new PersistentProxyBuilder());

        static void Main(string[] args)
        {
            ListString ls = new ListString();
            ls.Add("hello");

            List<string> ls2 = (ListString)_generator.CreateClassProxyWithTarget(typeof(ListString), ls, new Interceptor());

            var x = ls2[0];
        }
    }

    public class ListString : List<String>
    {
        public ListString() : base() { }
        public ListString(IEnumerable<String> strings) : base(strings) { }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用Castle.DynamicProxy;
名称空间DP
{
班级计划
{
私有静态只读ProxyGenerator _generator=new ProxyGenerator(new PersistentProxyBuilder());
静态void Main(字符串[]参数)
{
ListString ls=新ListString();
加上(“你好”);
List ls2=(ListString)_generator.CreateClassProxyWithTarget(typeof(ListString),ls,new Interceptor());
var x=ls2[0];
}
}
公共类ListString:List
{
public ListString():base(){}
公共ListString(IEnumerable字符串):基(字符串){}
}
}
请帮忙!它快把我逼疯了!我试过Castle 3.2和2.5,它们似乎都不起作用。我可以使用“普通”对象获得很好的结果。

列表上的索引器不是虚拟的,不能被DynamicProxy拦截


您可以代理IList。

很高兴知道。顺便说一句,我很喜欢你对城堡的呵护。