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 7994945
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T14:15:52+00:00 2026-06-04T14:15:52+00:00

I am having trouble getting Castle Dynamic Proxy to intercept methods that are explicit

  • 0

I am having trouble getting Castle Dynamic Proxy to intercept methods that are explicit interface implementations. I read here http://kozmic.pl/category/dynamicproxy/ that it should be possible to do this.
Here are my classes;

internal interface IDomainInterface
{
    string DomainMethod();
}

public class DomainClass : IDomainInterface
{
    string IDomainInterface.DomainMethod()
    {
        return "not intercepted";
    }
}

Here is my interceptor class;

public class DomainClassInterceptor : IInterceptor
{
    public void Intercept(IInvocation invocation)
    {
        if (invocation.Method.Name == "DomainMethod")
            invocation.ReturnValue = "intercepted";
        else
            invocation.Proceed();
    }
}

And here is my test (which fails);

    [TestClass]
    public void can_intercept_explicit_interface_implementation()
    {
        // Create proxy
        var generator = new ProxyGenerator();
        var interceptor = new DomainClassInterceptor();
        var proxy = (IDomainInterface)generator.CreateClassProxy(typeof(DomainClass), interceptor);

        // Invoke proxy method
        var result = proxy.DomainMethod();

        // Check method was intercepted -- fails
        Assert.AreEqual("intercepted", result);
    }

In addition to not being able to intercept the explicit interface implementation, it also seems that I am not receiving a notification of a non-proxyable member.
Here is my proxy generation hook (which acts as a spy);

public class DomainClassProxyGenerationHook : IProxyGenerationHook
{
    public int NonProxyableCount;

    public void MethodsInspected() {}

    public void NonProxyableMemberNotification(Type type, MemberInfo memberInfo)
    {
        NonProxyableCount++;
    }

    public bool ShouldInterceptMethod(Type type, MethodInfo methodInfo)
    {
        return true;
    }
}

Here is my test (which again fails);

    [TestMethod] 
    public void receive_notification_of_nonproxyable_explicit_interface_implementation()
    {
        // Create proxy with generation hook
        var hook = new DomainClassProxyGenerationHook();
        var options = new ProxyGenerationOptions(hook);
        var generator = new ProxyGenerator();
        var interceptor = new DomainClassInterceptor();
        var proxy = (IDomainInterface)generator.CreateClassProxy(typeof(DomainClass), options, interceptor);

        // Check that non-proxyable member notification was received -- fails
        Assert.IsTrue(hook.NonProxyableCount > 0);
    }

Has anyone had success in getting DP to intercept explicit interface implementations? If so, how?

  • 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-06-04T14:15:53+00:00Added an answer on June 4, 2026 at 2:15 pm

    You are creating a class proxy. Class proxy only intercepts virtual methods on the class, and an explicit implementation of an interface method in C# by definition is not virtual (since it’s private).

    If you want to intercept methods on the interface you need to explicitly tell DynamicProxy about it

    var proxy = (IDomainInterface)generator.CreateClassProxy(typeof(DomainClass), new Type[] { typeof(IDomainInterface) }, interceptor);
    

    Also your interface is marked as internal so made sure it’s public for DynamicProxy (either make the interface public or add InternalsVisibleToAttribute).

    With that your first test will pass, and the method will be intercepted.

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

Sidebar

Related Questions

I'm having trouble getting the Apache Camel plugin to run in grails-1.1.1. Here's the
Having trouble getting my JQuery POST to be accepted by the WCF Service. Here's
Having trouble getting this to work. What's strange is that I have 10 bookmarks
I'm having trouble getting Jquery .closest() to work. Here's my code, it's simple and
I am having trouble getting the Google Sitemap to automatically generate. Here’s what I
Having trouble getting the id of divs on my page that begin with refresh.
I'm having trouble getting a filter to work on a BindingSource that is the
Im having trouble getting this to work, here´s a quick overview of the idea.
I am having trouble getting the row of a WPF grid that a textbox
I am having trouble getting the hibernate criteria to execute, here is my code

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.