G
Guest
I'm having some problems comparing delegates. In all sample projects I
create, I can't get the problem to occur, but there is definitely a problem
with my production code.
I can't give all the code, as there's simply too much, but here's the
general gist:
I have a connection object which connects to a custom back-end server of one
type or another. Clients of this connection object send requests via a
method (e.g. connection.SendRequest()) and one of parameters to this call is
a callback delegate.
At a later point, the client may attempt to cancel all the requests specific
to the delegate that was passed in. It's in this method that I'm doing the
delegate comparisons. Consistently, the delegates never match, so the
requests can't be cancelled.
In the client code, I've tried passing in the method name directly or a
delegate instance, created with the method as a parameter. I get the same
results either way.
When debugging, I notice something strange, and no doubt the cause of the
problem:
The stored delegate refers to the actual method to call:
.Method: the actual method name
.Target: the actual class name where the method resides
....but the delegate passed in, refers to an Invoke method:
.Method: Invoke
.Target: the delegate type!
So, I understand why the comparison is failing. They don't appear to be the
same delegate at all! But I don't understand why the passed in delegate
refers to the Invoke method, instead of the actual target method.
Can someone explain to me what's going on? And moreover, does anyone have a
solution to the problem?
Thanks.
create, I can't get the problem to occur, but there is definitely a problem
with my production code.
I can't give all the code, as there's simply too much, but here's the
general gist:
I have a connection object which connects to a custom back-end server of one
type or another. Clients of this connection object send requests via a
method (e.g. connection.SendRequest()) and one of parameters to this call is
a callback delegate.
At a later point, the client may attempt to cancel all the requests specific
to the delegate that was passed in. It's in this method that I'm doing the
delegate comparisons. Consistently, the delegates never match, so the
requests can't be cancelled.
In the client code, I've tried passing in the method name directly or a
delegate instance, created with the method as a parameter. I get the same
results either way.
When debugging, I notice something strange, and no doubt the cause of the
problem:
The stored delegate refers to the actual method to call:
.Method: the actual method name
.Target: the actual class name where the method resides
....but the delegate passed in, refers to an Invoke method:
.Method: Invoke
.Target: the delegate type!
So, I understand why the comparison is failing. They don't appear to be the
same delegate at all! But I don't understand why the passed in delegate
refers to the Invoke method, instead of the actual target method.
Can someone explain to me what's going on? And moreover, does anyone have a
solution to the problem?
Thanks.