Comments (5)
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.
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.
@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.
@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.
@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)
- Random VerificationException/TypeLoadException HOT 5
- it's very slow to create proxy HOT 2
- Log4Net not listed in dependencies HOT 4
- `InvalidProgramException` when proxying `MemoryStream` with .NET 7 HOT 15
- Proxy created with CreateClassProxyWithTarget returns false for Equals on itself HOT 3
- Why TransactionInterceptor.Intercept run at the client HOT 2
- System.InvalidOperationException: Operations that change non-concurrent collections must have exclusive access. HOT 1
- `ArgumentException`: "Could not find method overriding method" with overridden class method having generic by-ref parameter HOT 3
- `ArgumentException`: "Cannot create an instance of `TEnum` because `Type.ContainsGenericParameters` is true" caused by `Enum` constraint on method `out` parameter HOT 4
- Proxy fail to create for create substitute that returns a derived class from abstract that satisfies interface implicitly HOT 5
- Release 5.2.0 HOT 1
- Support by-ref-like (`ref struct`) parameter types such as `Span<T>` and `ReadOnlySpan<T>` HOT 10
- Support "with" for record proxies HOT 4
- CreateInterfaceProxyWithoutTarget and BaseTypeForInterfaceProxy-member
- DictionaryAdapter Nesting Dictionaries JSON like
- Allow to intercept IAsyncEnumerable HOT 1
- ProxyGenerator leaking HOT 2
- MissingMethodException when trying to call a proxy target from different project
- .NET 9 compatibility: Signature of the body and declaration in a method implementation do not match HOT 1
- DI behavior change between .NET 7.0 and .NET 8.0 HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from core.