У меня есть следующий класс (созданный для Unity Game Engine)
using System;
using System.Collections.Generic;
using UnityEngine;
public class Biome : ScriptableObject, IEquatable<Biome>
{
// ...
//
// IEquatable
//
public bool Equals(Biome other)
{
if (other == null)
return false;
return this.name == other.name;
}
public override bool Equals(object obj)
{
if (obj == null) return false;
Biome other = obj as Biome;
if (other == null) return false;
return Equals(other);
}
public override int GetHashCode()
{
return this.name.GetHashCode();
}
public static bool operator ==(Biome biome1, Biome biome2)
{
if (object.ReferenceEquals(biome1, biome2)) return true;
if (object.ReferenceEquals(null, biome1)) return false;
if (object.ReferenceEquals(null, biome2)) return false;
return biome1.Equals(biome2);
}
public static bool operator !=(Biome biome1, Biome biome2)
{
if (object.ReferenceEquals(biome1, biome2)) return false;
if (object.ReferenceEquals(biome1, null)) return true;
if (object.ReferenceEquals(biome2, null)) return true;
return !biome1.Equals(biome2);
}
}
Когда я пытаюсь выполнить тест, функция Equals
, кажется, работает, но оператор ==
дает мнедругой результат.
[Test]
public void FooTest()
{
ScriptableObject biome1 = ScriptableObject.CreateInstance("Biome");
ScriptableObject biome2 = ScriptableObject.CreateInstance("Biome");
biome1.name = "Biome #1";
biome2.name = "Biome #1";
Assert.IsTrue(biome1.Equals(biome2));
Assert.IsTrue(biome1 == biome2); // This one fails
}
Я что-то не так делаю с моей реализацией?
ОБНОВЛЕНИЕ: Вот полный класс, если он что-то меняет: https://www.hastebin.com/divazubero.cpp