Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6109055
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:21:44+00:00 2026-05-23T14:21:44+00:00

I have run into a bit of (what I think is) strange behaviour when

  • 0

I have run into a bit of (what I think is) strange behaviour when using Castle’s Dynamic Proxy.

With the following code:

class Program
{
    static void Main(string[] args)
    {
        var c = new InterceptedClass();
        var i = new Interceptor();

        var cp = new ProxyGenerator().CreateClassProxyWithTarget(c, i);

        cp.Method1();
        cp.Method2();

        Console.ReadLine();
    }
}

public class Interceptor : IInterceptor
{
    public void Intercept(IInvocation invocation)
    {
        Console.WriteLine(string.Format("Intercepted call to: " + invocation.Method.Name));

        invocation.Proceed();
    }
}

public class InterceptedClass
{
    public virtual void Method1()
    {
        Console.WriteLine("Called Method 1");
        Method2();
    }

    public virtual void Method2()
    {
        Console.WriteLine("Called Method 2");
    }
}

I was expecting to get the output:

  • Intercepted call to: Method1
  • Called Method 1
  • Intercepted call to: Method2
  • Called Method 2
  • Intercepted call to: Method2
  • Called Method 2

However what I got was:

  • Intercepted call to: Method1
  • Called Method 1
  • Called Method 2
  • Intercepted call to: Method2
  • Called Method 2

As far as I can tell then the dynamic proxy is only able to proxy method calls if the call comes from outside the class itself as Method2 was intercepted when called from Program but not from within InterceptedClass.

I can kind of understand that when making calls from within the proxied class it would no longer go through the proxy, but just wanted to check that this was expected and if it is then see if there is there anyway to get all calls intercepted regardless of where they’re called from?

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T14:21:45+00:00Added an answer on May 23, 2026 at 2:21 pm

    EDIT: tl;dr – I’ve just tried creating the proxy in a different way, as described below, and it produces the output you were after. I just had to change this:

    var c = new InterceptedClass();
    var i = new Interceptor();
    
    var cp = new ProxyGenerator().CreateClassProxyWithTarget(c, i);
    

    To this:

    var i = new Interceptor();
    var cp = new ProxyGenerator().CreateClassProxy<InterceptedClass>(i);
    

    As I understand it, the proxy generator is effectively creating a wrapper object. They’re two separate objects – one is just a wrapper around the other, with interception etc in the wrapper layer.

    It’s hard to see how it could change what the instance of InterceptedClass did with its own method calls:

    • DynamicProxy can’t change the type of an existing object; once an object is created, its type is fixed
    • DynamicProxy can’t change how existing calls to an existing object are bound

    If you want Method1 to call Method2 via the wrapper using the current proxy creation code, you’ll need to tell the existing object about the wrapper, either as a field within it or as a method parameter.

    Alternatively, there may be a different way of creating the proxy to start with – one where the proxy is in some sense the target object. I suspect you may want to look at CreateClassProxy rather than CreateClassProxyWithTarget – I suspect it’s the fact that you’re supplying the target object which is causing you problems.

    Whether the behaviour you’re seeing is “expected” or not obviously depends on your expectations – but it’s certainly what I would expect, without knowing anything about Castle Dynamic Proxy 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have run into a bit of a design issue with my code. I
I have run into a bit of a snag in my code. I am
I have run into a bit of a problem here: I had a problem-specific
I'm rather new to Backbone.js development, and have run into a bit of a
I am working on bit shifts, and I've run into this problem. I have
Im trying to learn a bit about c++ and have run in to some
I have run into a problem with Eclipse Indigo, attempting to start up my
I have run into a little problem. I am connecting to a webservice that
I have run into a couple of similar quirks regarding uint usage in both
I have run into a situation where I want to ensure that a compound

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.