Testing delegates for equality
Asked Answered
X

3

15

I'm building a hierarchical collection class that orders magnetic resonance images spatially and arranges them into groupings based on the various acquisition parameters that were used to generate them. The specific method used to perform the grouping is provided by the user of the class. I've abstracted out the relevant features in the sample code below. For the IEquatable<MyClass> implementation, I'd like to be able to compare the _myHelperDelegate attributes of two MyClass instances to determine if both delegates point to the same piece of code. The (_myHelperDelegate == other._myHelperDelegate) clause in the if statement below is clearly the wrong way to go about doing this (it fails to compile, giving the error "Method name expected"). My question is, is there a way to compare two delegates to determine if they reference the same piece of code? If so, how do you do that?

public class MyClass : IEquatable<MyClass>
{
   public delegate object HelperDelegate(args);
   protected internal HelperDelegate _myHelperDelegate;

   public MyClass(HelperDelegate helper)
   {
      ...
      _myHelperDelegate = helper;
   }

   public bool Equals(MyClass other)
   {
      if (
          (_myHelperDelegate == other._myHelperDelegate) &&
          (... various other comparison criteria for equality of two class instances... )
         )
         return true;
      return false;
   }
}
Ximenez answered 4/3, 2011 at 19:59 Comment(4)
"I'm building a hierarchical collection class that orders magnetic resonance images spatially" you've lost me.Fahrenheit
The Delegate class does have an Equals() method (msdn.microsoft.com/en-us/library/99bthb1z(v=VS.90).aspx), did you try that?Runner
My intuition about the source of the problem was incorrect. The comparison of the two delegates using == actually does work. The problem was a syntax error in the original code. I was missing the && between clauses in the if statement. facepalmXimenez
@Yuriy - The spatial arrangement of the images was tangential to the actual problem and could have been omitted without loss of generality, except I wanted to indicate that the comparison of two delegates had a real-world purpose in an attempt to preemptively avoid comments of the, "Why on earth would you want to do that?" variety.Ximenez
S
5

The following compiles and works as expected.

private void Form1_Load(object sender, EventArgs e)
{
    var helper1 = new TestDelegates.Form1.MyClass.HelperDelegate(Testing);
    var helper2 = new TestDelegates.Form1.MyClass.HelperDelegate(Testing2);
    var myClass1 = new MyClass(helper1);
    var myClass2 = new MyClass(helper1);

    System.Diagnostics.Debug.Print(myClass1.Equals(myClass2).ToString()); //true

    myClass2 = new MyClass(helper2);
    System.Diagnostics.Debug.Print(myClass1.Equals(myClass2).ToString()); //false

}

private object Testing()
{
    return new object();
}
private object Testing2()
{
    return new object();
}

public class MyClass : IEquatable<MyClass>
{
   public delegate object HelperDelegate();
   protected internal HelperDelegate _myHelperDelegate;

   public MyClass(HelperDelegate helper)
   {
     _myHelperDelegate = helper;
   }

   public bool Equals(MyClass other)
   {
      if (_myHelperDelegate == other._myHelperDelegate)
      {
         return true;
      }
      return false;
   }
}
Segment answered 4/3, 2011 at 20:24 Comment(0)
M
4

Per msdn, Delegate.Equals method returns:

true if obj and the current delegate have the same targets, methods, and invocation list; otherwise, false.

Have you tried this?

Mapes answered 4/3, 2011 at 20:5 Comment(1)
More likely MulticastDelegate implementation of that method.Fahrenheit
P
0

Old question, but I wrote a simple example program to demonstrate comparing delegates with Delegate.Equals -

public delegate int test1(int t);

public static int asdf(int t)
{
    return t + 5;
}
public static int asdf2(int x)
{
    return x + 7;
}
public static void CompareDelegates(test1 test1, test1 test2)
{
    Console.WriteLine(test1 == test2);
}

public static void Main(string[] args)
{
    test1 test1 = asdf;
    test1 test2 = asdf2;
    test1 test3 = asdf;
    
    CompareDelegates(test1, test1);
    CompareDelegates(test1, test2);
    CompareDelegates(test2, test3);
    CompareDelegates(test1, test3);     
}

// Outputs:
//
// True
// False
// False
// True
Pier answered 15/9, 2022 at 23:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.