【问题标题】:How can I create a dynamic proxy out of a List<T>如何从 List<T> 创建动态代理
【发布时间】:2013-09-17 02:57:54
【问题描述】:

我正在使用 Castle Dynamic Proxy 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) { }
    }
}

请帮忙!它快把我逼疯了!我尝试了 Castle 3.2 和 2.5,它们似乎都不起作用。我可以用“正常”的对象达到很好的效果。

【问题讨论】:

    标签: c# castle castle-dynamicproxy dynamic-proxy


    【解决方案1】:

    List&lt;T&gt; 上的索引器不是虚拟的,无法被 DynamicProxy 拦截。

    您可以改为代理IList&lt;T&gt;

    【讨论】:

    • 很高兴知道。顺便说一句,我很喜欢你在 Castle 上的表演。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-07-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-19
    • 2012-06-16
    • 1970-01-01
    • 2015-09-03
    相关资源
    最近更新 更多