Giter Club home page Giter Club logo

Comments (5)

joelweiss avatar joelweiss commented on August 17, 2024

In other words I want the proxy to be generated as follows

public class Blog
{
    public virtual int BlogId { get; set; }

    public virtual void SetBlogId(int blogId) => BlogId = blogId;
}

public class BlogProxy : Blog, INotifyPropertyChanged
{
    private readonly Blog _Target;

    public BlogProxy(Blog target) => _Target = target;

    public override int BlogId
    {
        get => _Target.BlogId;
        set
        {
            _Target.BlogId = value;
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(BlogId)));
        }
    }

    // This I don't know how to achieve in > 5.0.0
    public override void SetBlogId(int blogId) => base.SetBlogId(blogId);

    public event PropertyChangedEventHandler? PropertyChanged;
}

----

blog = new Blog();
Blog blogProxy = new BlogProxy(blog);
bool blogProxyRaised = false;
((INotifyPropertyChanged)blogProxy).PropertyChanged += (o, e) => blogProxyRaised = true;
blogProxy.SetBlogId(5000);
Console.WriteLine(blogProxyRaised ? "[Proxy] Change event was raised as expected" : "[Proxy] Expected a change event");

from core.

stakx avatar stakx commented on August 17, 2024

Responding to your first post:

Is there any way I can invoke the target.Method() on the Proxy?

I don't think so. The target object, which is a plain Blog object, doesn't know about the proxy, it's just your code; so there isn't going to be any magical redirection from your code back to the proxy.

I suspect the real problem here is that object state gets split across two object instances, which happens because you've chosen to use a composition-based proxy (CreateClassProxyWithTarget). You might be more successful with an inheritance-based proxy (CreateClassProxy), where you'll end up with only a single object.

from core.

joelweiss avatar joelweiss commented on August 17, 2024

@stakx thanks for the response

CreateClassProxy will not work in my case, because I am proxying an existing class with data already, hence CreateClassProxyWithTarget

from core.

stakx avatar stakx commented on August 17, 2024

@joelweiss, sorry for the slow reply. Back in January, I suspected that there's nothing further we can do for you, but I wanted to sleep on it in case I could come up with something... and then I forgot.

To answer your core question, which was how to do an invocation.Proceed() that proceeds to the base class' method implementation, instead of to the target's:

// This I don't know how to achieve in > 5.0.0
public override void SetBlogId(int blogId) => base.SetBlogId(blogId);

DynamicProxy doesn't have an API for that. Also, you cannot do it using Reflection like this (inside your interceptor):

if (invocation.Method.Name == "SetBlogId")
{
    var baseMethod = invocation.Proxy.GetType().BaseType.GetMethod("SetBlogId");
    baseMethod.Invoke(invocation.Proxy, invocation.Arguments);
}

The problem here is that baseMethod.Invoke will perform a virtual dispatch, which just makes execution end up in the proxy and you get an infinite loop.

You could implement an alternate Invoke helper that does not perform virtual dispatch, so you'd really end up in the base class' method implementation. I have previously implemented a DynamicInvokeNonVirtually helper method for Moq that does just that. You could use that (in the above example interceptor code) instead of baseMethod.Invoke.

I'm not really sure whether your scenario is an edge case, or whether it might be useful to extend DynamicProxy's API with something like invocation.ProceedToBase() that would do what you asked for directly.

Any opinions, anyone?

from core.

joelweiss avatar joelweiss commented on August 17, 2024

@stakx thanks for the response.

yes, invocation.ProceedToBase() is exactly what I am looking for (that was the behavior before 5.0.0).
In the mean time I will have a look if your suggestion to call DynamicInvokeNonVirtually will work for me.

Thanks again.

from core.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.