我有两个具有相同元素和值的对象列表:
parameters = new List<Parameter>() { new Parameter() { parameterName="value", parameterType="string"} }
Parameter类如下所示:
public class Parameter
{
public string parameterName;
public string parameterType;
}
我想通过单元测试将其与具有相同元素和值的相同对象列表进行比较。
我的方法类(我在方法对象之前创建,该对象存储了Parameter对象的列表):
public class Method
{
public List<Parameter> parameters { get; set;}
public string modifier { get; set; }
public string name { get; set; }
public string type { get; set; }
public override bool Equals(object obj)
{
return this.modifier == ((Method)obj).modifier && this.name == ((Method)obj).name && this.type == ((Method)obj).type
&& this.parameters == ((Method)obj).parameters;
}
}
我的问题是在equals方法中,在this.parameters == ...:
public override bool Equals(object obj)
{
return this.modifier == ((Method)obj).modifier && this.name == ((Method)obj).name && this.type == ((Method)obj).type
&& this.parameters == ((Method)obj).parameters;
}
顺便说一句,此方法中的所有其他条件都有效。与对象相比,修饰符,名称和类型返回正确的值。因为这些是基本的字符串值,所以比较容易得多。当我要对对象列表执行相同操作时,它会变得更加复杂。
如何在此Equals()方法中比较两个参数对象列表,我是否需要比较每个元素(例如比较列表中所有对象的parameterName和parameterType)?
参考方案
您需要在Equals
类中覆盖Parameter
并覆盖GetHashCode
才能保持哈希值(否则,对于具有相同值的2个对象,您将获得不同的哈希值)。
public class Parameter
{
public string parameterName;
public string parameterType;
public override bool Equals(object obj)
{
return parameterName == ((Parameter) obj)?.parameterName &&
parameterType == ((Parameter) obj)?.parameterType;
}
public override int GetHashCode()
{
unchecked
{
var hashCode = parameterName != null ? parameterName.GetHashCode() : 0;
hashCode = (hashCode * 397) ^ (parameterType != null ? parameterType.GetHashCode() : 0);
return hashCode;
}
}
}
然后在比较列表时使用SequenceEqual
:
public class Method
{
public List<Parameter> parameters { get; set; }
public string modifier { get; set; }
public string name { get; set; }
public string type { get; set; }
public override bool Equals(object obj)
{
return modifier == ((Method)obj)?.modifier &&
name == ((Method)obj)?.name &&
type == ((Method)obj)?.type &&
( parameters == null && ((Method)obj)?.parameters == null ||
parameters != null && ((Method)obj)?.parameters != null && parameters.SequenceEqual(((Method)obj).parameters));
}
public override int GetHashCode()
{
unchecked
{
var hashCode = parameters != null ? parameters.GetHashCode() : 0;
hashCode = (hashCode * 397) ^ (modifier != null ? modifier.GetHashCode() : 0);
hashCode = (hashCode * 397) ^ (name != null ? name.GetHashCode() : 0);
hashCode = (hashCode * 397) ^ (type != null ? type.GetHashCode() : 0);
return hashCode;
}
}
}
尝试将JS &&运算符转换为C# - javascript我有要转换为C#的JS代码。由于某种原因,我的C#方法返回的值比JS函数的返回值小10。我尝试更改多个内容并检查JS中&&运算符的含义,但似乎无法弄清楚我在做什么错。正确的返回值为97。JavaScript功能和用法:function rir(t, e, c, n) { return t > e && t <= c…
如何使用大量查询字符串验证网址 - php我有一个自动网站,并且有一个名为“保存搜索”的功能,该功能可以保存用户在该网站上进行的搜索以备将来使用。我正在做的是将整个URL保存在数据库中。但是我需要将其验证为有效的网址。看起来像这样。http://www.anywebsite.com/cars.php?postcode=CV2+5AS&distance=&make=&min_p…
JVM如何运行if(condition && false)语句? - java目前,我正在开发一个具有内存密集检查过程的程序。在某些时候,代码看起来像这样。if(isvalid() && false) //this false is acctually another method which will at this given //point always return false { //rest ommited…
从request.querystring中删除字符 - c#我有一些代码从类似下面的网址中获取DrawingId:/Queue/Queue.aspx?Mode=Autosearch&DrawingID=188320“我的问题是在运行程序的另一部分后,该网址变成了:/Queue/Queue.aspx?Mode=Autosearch&DrawingID=188320?DrawingRequestId=37…
无法访问PHP中的参数,无法使用序列化从jQuery发布值 - javascript我正在通过AJAX Post从jquery发送数据,并将所有表单字段序列化(在WordPress中)。$('#SendEmail').click(function () { jQuery.post( the_ajax_script.ajaxurl, { 'action': 'the_ajax_hook_Sen…