473,416 Members | 1,869 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,416 software developers and data experts.

Delegate comparisons

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.
Apr 20 '06 #1
1 1799
Here is an except from a discussion in another forum on this same topic.
Note, I still haven't resolved the problem.

Here's the basic structure of the code I'm using. Note, I don't have a
problem with this code; it appears to be working as expected. When I run the
same sort of
structure on my code, the delegates are never equal, as described in my first
post. The problem is, when I'm doing the same sort of thing in my code.

namespace DelegateTest
{
delegate void MyDelegate(int x);

class Program
{
static void Main(string[] args)
{
Client client = new Client();
client.Start();
}
}

class Client
{
Connection connection = new Connection();

public void Callback(int x)
{
}

public void Start()
{
MyDelegate myDel = new MyDelegate(Callback);

connection.SendRequest(myDel);
connection.SendRequest(Callback);
connection.CancelRequests(myDel);
connection.CancelRequests(Callback);
}
}

class Connection
{
List<MyDelegate> storedDelegates = new List<MyDelegate>();

public void SendRequest(MyDelegate del)
{
storedDelegates.Add(del);
// Send request to server
}

public void CancelRequests(MyDelegate del)
{
Console.WriteLine("Cancel requests:");
int count = 0;
foreach (MyDelegate storedDel in storedDelegates)
{
count++;
Console.WriteLine("Delegate: " + count.ToString());
Console.WriteLine((del == storedDel).ToString());
Console.WriteLine(del.Equals(storedDel).ToString() );
Console.WriteLine(MyDelegate.Equals(del,
storedDel).ToString());
Console.WriteLine(MyDelegate.ReferenceEquals(del,
storedDel).ToString());
}
// Send cancel to the server
}
}
}

....And now the discussion:
your code is a bit confusing. In one case, you're adding a delegate to the
list. In another, you're adding a method:
connection.SendRequest(myDel);
connection.SendRequest(Callback);
They are not the same. A delegate is a delegate; a method is a method.


Yes, I did this on purpose to demonstrate the different ways in which I have
tried to resolve the problem. When a parameter to a method is a delegate,
..NET allows me to either specify an explicit delegate OR a method matching
that delegate as the parameter, as I'm sure you're aware. It seems like
..NET 2.0 allows you to treat delegates and methods almost exactly the same.

I was just trying to test if I would get different results in my
comparisons, depending on which method I use (explicit delegate or method
name) to call SendRequest().
You have not posted your output, so I have no idea what the answer to the
following question is:


My output, I'm sure, would be the same as what you would get compiling and
running this code:
Cancel requests:
Delegate: 1
True
True
True
True
Delegate: 2
True
True
True
False
Cancel requests:
Delegate: 1
True
True
True
False
Delegate: 2
True
True
True
False

This is expected behavior, so my sample code isn't showing the problem that
I'm experiencing. It just illustrates the basic structure of the code I'm
debugging.
Any thoughts on why that Invoke is getting in there?


What Invoke getting in where?


Here's the scenario:
In my actual client code, I call the SendRequest() method a few times, each
time passing in the same delegate (or the same method name, when I tried it
that way), and then called the CancelRequests() method, passing in that same
delegate (or method name) -- exactly like I'm doing in my sample code above.

In VS, I put a break point in the CancelRequests() method, at the point
where the comparison between the passed-in delegate and the stored delegate
were compared. The equality comparison between these two was always
returning false, depsite the fact that I passed in the actual same delegate
to both the SendRequest() and CancelRequests() methods. This is what's
puzzling. I'm trying a few different things to get the proper comparison to
happen:

At compile-time, if I try to compare (using any of the equality comparisons
mentioned) the passed-in delegate with the stored delegate, I get the
following compile-time error:

The best overloaded method match for 'object.Equals(object, object)' has
some invalid arguments: Argument '1': cannot convert from 'method group' to
'object'

So, that makes sense. This is where the natural .NET facility to treat
methods and delegates the same breaks down. But this is in itself is
puzzling, because the the stored delegate reference is of the same type.
The reference inside the object is a delegate reference, so I don't
understand why .NET is now treating it as a method instead, when I'm doing
the comparison. The stored reference is in a hash table of request objects,
and inside each of those request objects is a delegate reference. That
reference is set to what was initialled passed in to SendRequest().

In an attempt to get around this problem, I create a new, temporary delegate
just before the comparison. The method I give as a parameter to the temp
delegate constructor is the stored delegate reference from inside the request
object.

When I inspect the two delegates at debug-time, here's what I see:

Stored delegate (direct reference to it from the hash table):
Type: my delegate type
Base: System.MulticastDelegate
Base: System.Delegate
Method: Void Callback(int x)
Target: null

Passed-in delegate:
Type: my delegate type
Base: System.MulticastDelegate
Base: System.Delegate
Method: Void Callback(int x)
Target: null

They look the same, as far as I can tell. But, remember, I am unable to
find a comparison method which will compare these directly and not give
compiler errors, since .NET is treating the stored delegate reference as a
'method group' and not a delegate!

So, as I explained, I wrapped up the stored delegate in a temporary delegate
and inspected it at debug time. This is what I get:
Type: my delegate type
Base: System.MulticastDelegate
Base: System.Delegate
Method: Void Callback(int x)
Target: Request object

And a comparison between this and the passed-in delegate fails (I presume)
because of the different targets.

So, in desperation, I ALSO wrapped the passed-in delegate in a seperate
temporary delegate. At debug-time it looked like this:
Type: my delegate type
Base: System.MulticastDelegate
Base: System.Delegate
Method: Void Invoke(int x)
Target: My delegate type

So, that's where the Invoke comes in. I assume then that me wrapping the
passed-in delegate in a temporary delegate, adds an extra delegate layer, and
isn't what I want.

Apr 21 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Dietrich Epp | last post by:
Without invoking double-underscore black magic, it is possible to choose a, b, and c in Python such that: a < b b < c c < a This could cause sorting functions to malfunction. >>> class...
3
by: Kevin King | last post by:
I have a question about an assignment I have. I need to count the number of comparisons in my merge sort. I know that the function is roughly nlog(n), but I am definately coming up with too many...
2
by: Russell Hind | last post by:
I have a delegate which I use to store a current 'state' function (for a statemachine inside a form). __delegate void State_t(const Message_c& Message); I assign to it such as m_State = new...
3
by: Minh Khoa | last post by:
Please give me more information about delegate and its usage? Why do i use it and when?
4
by: ^MisterJingo^ | last post by:
Hi all, I've been trying to get my head around delegates. The book i'm using had a single example, not much explaination, and didn't show how to set up a delegate and pass variables in and out...
7
by: Ant | last post by:
Hello, Very simple question but one I need clarified. Which part of the statement below is considered the 'delegate'? Is it the 'new System.EventHandler' or the btnAccept_Click? or is it...
11
by: matsi.inc | last post by:
I am looking to make something like a delegate that i can use in my projects but am having a hard time getting started. The behavior I am most interested in is how a delegate changes it's Invoke...
26
by: raylopez99 | last post by:
Here is a good example that shows generic delegate types. Read this through and you'll have an excellent understanding of how to use these types. You might say that the combination of the generic...
10
by: vcquestions | last post by:
Hi. Is there way to have a function pointer to a delegate in c++/cli that would allow me to pass delegates with the same signatures as parameters to a method? I'm working with managed code. ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.