1

Is there a way to create a proxy of a delegate type and have it implement additional interfaces in DynamicProxy2 and also being able to intercept calls to the generated delegate?

The way i normaly generate proxies throws an exception because delegate types are sealed.

Patrik Hägne
  • 16,751
  • 5
  • 52
  • 60
  • 1
    As follow up to my answer. In DP 2.2 it will be much easier (ok, seriously, now it is really hard) to extend the current proxy generators and/or add new ones (there's a branch in the repository for this if you want to look at the code). So you'll be able to create a type that has a method with signature you choose (like the one of the delegate type you're targeting) and expose the method on the instance via new delegate that wraps the delegate provided by user. This should set you up for the general case. Email me for details if you want to continue this discussion. – Krzysztof Kozmic Sep 10 '09 at 19:13
  • Just did email you as a matter of fact, this sounds interesting, awaiting your reply! – Patrik Hägne Sep 10 '09 at 19:45

1 Answers1

0

Patrik,

You don't need DynamicProxy to 'proxy' delegates This should be enough:

Action delegateToproxy = new Foo().Bar; //Bar is public void Bar(){}
Action proxy = delegate
               {
                  Console.WriteLine("Intercepted!");
                  delegateToProxy();
               }
return proxy;

[UPDATE: that aswer was not relevant to this specific problem] What kind of API do you want to expose for this?

If you want to follow up with this conversation please contact me via email, or start a thread on Castle users group.

Krzysztof Kozmic
  • 27,267
  • 12
  • 73
  • 115
  • As you say in your update, this is only half way there, the problem that remains is that I can't have it implement any extra interfaces. I will mark this answer as the accepted one though since it's a good answer in the general case. – Patrik Hägne Sep 10 '09 at 16:55