I'm using Castle Dynamic Proxy CreateClassProxyWithTarget using an already existing list. Basically, I'd like to to intercept the calls being made to the list indexer.
I've tried a bunch of combination to achieve this goal but each time the created proxy returns an empty list.
For example :
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) { }
}
}
Please help! It's driving me mad ! I tried Castle 3.2 and 2.5, none of them seem to work. I can achieve good result with "normal" objects.
Indexer on List<T>
is not virtual and cannot be intercepted by DynamicProxy.
You can proxy IList<T>
instead.