具有数组属性的类的C#等式
我有以下价值具有数组属性的类的C#等式,c#,C#,我有以下价值 public class Identification : IEquatable<Identification> { public int Id { get; set; } public byte[] FileContent { get; set; } public int ProjectId { get; set; } } 但是当我想进行单元测试时,它在从存储库返回之前和之后是相等的,它失败了。尽管在失败消息中具有完全相同的属性 var i
public class Identification : IEquatable<Identification>
{
public int Id { get; set; }
public byte[] FileContent { get; set; }
public int ProjectId { get; set; }
}
但是当我想进行单元测试时,它在从存储库返回之前和之后是相等的,它失败了。尽管在失败消息中具有完全相同的属性
var identification = fixture
.Build<Identification>()
.With(x => x.ProjectId, projet.Id)
.Create();
await repository.CreateIdentification(identification);
var returned = await repository.GetIdentification(identification.Id);
var标识=夹具
.Build()
.With(x=>x.projectd,projet.Id)
.Create();
等待存储库。创建标识(标识);
返回的var=await repository.GetIdentification(identification.Id);
Assert.Equal()失败
应为:标识{FileContent=[56192243],Id=8,ProjectId=42}
实际:标识{FileContent=[56192243],Id=8,ProjectId=42}
如果有必要的话,我会将Npgsql与Dapper一起使用。对于检查以下内容的数组,应该使用
Enumerable.SequenceEqual
:
- 两个数组都是
,或者两个数组都不是null
null
- 两个数组的
长度相同
- 对应的项目彼此相等
public bool Equals(Identification other)
{
if (ReferenceEquals(null, other))
return false;
else if (ReferenceEquals(this, other))
return true;
return Id == other.Id &&
ProjectId == other.ProjectId &&
Enumerable.SequenceEqual(FileContent, other.FileContent);
}
由于
Enumerable.SequenceEqual
很可能会消耗时间,因此我已将其移到比较的末尾(如果ProjectId
未能相等,则无需检查数组)数组比较是引用相等,只需将其替换为SequenceEquals()
public bool Equals(Identification other)
{
if (ReferenceEquals(null, other))
return false;
else if (ReferenceEquals(this, other))
return true;
return Id == other.Id &&
ProjectId == other.ProjectId &&
Enumerable.SequenceEqual(FileContent, other.FileContent);
}