-
Notifications
You must be signed in to change notification settings - Fork 470
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Not Intercepted Method is invoked on target, not on proxy. #647
Comments
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"); |
Responding to your first post:
I don't think so. The target object, which is a plain 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 ( |
@stakx thanks for the response
|
@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
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 You could implement an alternate 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 Any opinions, anyone? |
@stakx thanks for the response. yes, Thanks again. |
This is a simplified version of what I am doing, but this should be enough to explain.
I want to add
INotifyPropertyChanged
to a POCO (Blog
) class,and I want to raise
PropertyChanged
even if the property is changed by a method on theTarget
(SetBlogId()
), this no longer works after 5.0.0 probably because #571 changed theSetBlogId()
execution to be on the target not on the proxy.Is there any way I can invoke the target.Method() on the Proxy?
in 4.4.1 the output is
Change event was raised as expected
in 5.0.0 the output is
Expected a change event
The text was updated successfully, but these errors were encountered: