Last active
August 31, 2020 08:35
-
-
Save sakapon/dca0caed09eb950c714c96fffa9c7aec to your computer and use it in GitHub Desktop.
OperatorsSample/VectorEq class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Reflection; | |
namespace OperatorsLib.Classes | |
{ | |
public abstract class EquatableObject | |
{ | |
// ValueType と同様に、すべてのフィールドで等価性を評価します。 | |
public override bool Equals(object obj) | |
{ | |
var type = GetType(); | |
if (type != obj?.GetType()) return false; | |
foreach (var field in type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)) | |
if (!Equals(field.GetValue(this), field.GetValue(obj))) return false; | |
return true; | |
} | |
public override int GetHashCode() | |
{ | |
HashCode hc = default; | |
foreach (var field in GetType().GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)) | |
hc.Add(field.GetValue(this)); | |
return hc.ToHashCode(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace OperatorsLib.Classes | |
{ | |
public class VectorEq : EquatableObject | |
{ | |
public double X { get; } | |
public double Y { get; } | |
public VectorEq(double x, double y) => (X, Y) = (x, y); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment