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

  • Home
  • SEARCH
  • 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 818965
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T02:13:53+00:00 2026-05-15T02:13:53+00:00

I need to use Castle DynamicProxy to proxy an interface by providing an instance

  • 0

I need to use Castle DynamicProxy to proxy an interface by providing an instance of it to ProxyGenerator.CreateInterfaceProxyWithTarget. I also need to make sure that calls to Equals, GetHashCode and ToString hits the methods on the concrete instance, that I am passing, and I can’t get that to work.

In other words, I’d like this small sample to print True twice, while in fact it prints True,False:

using System;
using Castle.Core.Interceptor;
using Castle.DynamicProxy;

public interface IDummy
{
    string Name { get; set; }
}

class Dummy : IDummy
{
    public string Name { get; set; }

    public bool Equals(IDummy other)
    {
        if (ReferenceEquals(null, other)) return false;
        if (ReferenceEquals(this, other)) return true;
        return Equals(other.Name, Name);
    }

    public override bool Equals(object obj)
    {
        return Equals(obj as IDummy);
    }      
}

class Program
{
    static void Main(string[] args)
    {
        var g = new ProxyGenerator();
        IDummy first = new Dummy() {Name = "Name"};
        IDummy second = new Dummy() {Name = "Name"};
        IDummy firstProxy = g.CreateInterfaceProxyWithTarget(first, new ConsoleLoggerInterceptor());
        IDummy secondProxy = g.CreateInterfaceProxyWithTarget(second, new ConsoleLoggerInterceptor());

        Console.WriteLine(first.Equals(second));         
        Console.WriteLine(firstProxy.Equals(secondProxy));
    }
}

internal class ConsoleLoggerInterceptor : IInterceptor
{
    public void Intercept(IInvocation invocation)
    {
        Console.WriteLine("Invoked " + invocation.Method.Name);
    }
}

Is this possible with DynamicProxy ? 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-05-15T02:13:54+00:00Added an answer on May 15, 2026 at 2:13 am

    This is a bit tricky. Take a look at documentation on how proxies work. Interface proxies wrap an object and intercept calls to designated interface(s). Since Equals is not part of that interface the second call to equals is comparing proxies, not their targets.

    So what provides the implementation for the second Equals call?

    Proxy is just another class implementing your IDummy interface. As any class it also has a base class, and that’s the base implementation of Equals that gets invoked. This base class is by default System.Object

    I hope you see now where this is going. Solution to this problem is to tell proxy to implement some proxy aware base class that will forward the calls to proxy target. Part of its implementation might look like this:

    public class ProxyBase
    {
        public override bool Equals(object obj)
        {
            var proxy = this as IProxyTargetAccessor;
            if (proxy == null)
            {
                return base.Equals(obj);
            }
            var target = proxy.DynProxyGetTarget();
            if (target == null)
            {
                return base.Equals(obj);
            }
            return target.Equals(obj);
        }
        // same for GetHashCode
    }
    

    Now you only need to instruct the proxy generator to use this base class for your interface proxies, instead of the default.

    var o = new ProxyGenerationOptions();
    o.BaseTypeForInterfaceProxy = typeof(ProxyBase);
    IDummy firstProxy = g.CreateInterfaceProxyWithTarget(first, o);
    IDummy secondProxy = g.CreateInterfaceProxyWithTarget(second, o);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 496k
  • Answers 496k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Create an array of 8-byte longs that has 2^16 entries.… May 16, 2026 at 11:43 am
  • Editorial Team
    Editorial Team added an answer You can use -[NSString stringWithFormat:]: NSString *body = [NSString stringWithFormat:@"<Item… May 16, 2026 at 11:43 am
  • Editorial Team
    Editorial Team added an answer ScaffoldColumn only changes the behavior of methods like Html.DisplayForModel() which… May 16, 2026 at 11:43 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I wanted to use the fluent interface in Castle Windsor. Since this isn't available
Assume you are using some libraries like NHibernate or Castle ActiveRecord that use log4net
Let's say that I'm currently designing an application where I will need to use
I am using Castle.DynamicProxy2 and I am instantiating my proxy as such: private static
This is pretty much a duplicate question but instead of using Castle Dynamic Proxy
I need to use a regex pattern , but what is the right php
I need to use C# to search a directory (C:\Logs) for log files whose
I need to use a 3rd party dll in our main app. When I
I need to use a number (a Rational ) as the log type of
I am very new to git but I need to use it for one

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.