测试代表是否平等

Testing delegates for equality(测试代表是否平等)
本文介绍了测试代表是否平等的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个分层集合类,它对磁共振图像进行空间排序,并根据用于生成它们的各种采集参数将它们分组.用于执行分组的具体方法由类的用户提供.我已经在下面的示例代码中抽象出了相关功能.对于 IEquatable 实现,我希望能够比较两个 MyClass 实例的 _myHelperDelegate 属性,以确定两个代表是否指向同一段代码.下面 if 语句中的 (_myHelperDelegate == other._myHelperDelegate) 子句显然是错误的方法(它无法编译,给出错误Method name expected").我的问题是,有没有办法比较两个代表来确定他们是否引用了同一段代码?如果是这样,你是怎么做到的?

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;
   }
}

推荐答案

以下编译并按预期工作.

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;
   }
}

这篇关于测试代表是否平等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)