Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Ddd.Taxi.Domain;
- using System.Reflection;
- using System.Text;
- using System.Collections.Generic;
- using System.Linq;
- namespace Ddd.Infrastructure
- {
- public class ValueType<T>
- {
- private readonly List<PropertyInfo> propertyInfos;
- public ValueType()
- {
- propertyInfos = GetType()
- .GetProperties(BindingFlags.Instance | BindingFlags.Public)
- .OrderBy(property => property.Name)
- .ToList();
- }
- public override int GetHashCode()
- {
- unchecked
- {
- int hash = 0;
- foreach (var property in propertyInfos)
- {
- var propertyValue = property.GetValue(this, null);
- var propertyHash = propertyValue != null ? propertyValue.GetHashCode() : 0;
- hash = (hash * 1244324135) ^ propertyHash;
- }
- return hash;
- }
- }
- public override bool Equals(object obj)
- {
- if (obj is null)
- return false;
- if (obj is not ValueType<T> otherValue)
- return false;
- foreach (var property in propertyInfos)
- {
- if (property.GetValue(this) == null && property.GetValue(otherValue) == null)
- continue;
- if (!(property.GetValue(this) == null || property.GetValue(otherValue) == null || !property.GetValue(this)
- .Equals(property.GetValue(otherValue))))
- continue;
- return false;
- }
- return true;
- }
- public bool Equals(PersonName name) => Equals((object)name);
- public override string ToString()
- {
- var result = new StringBuilder(GetType().Name + "(");
- for (int index = 0; index < propertyInfos.Count; index++)
- {
- var property = propertyInfos[index];
- var propertyValue = property.GetValue(this, null);
- result.AppendFormat("{0}: {1}{2}", property.Name, propertyValue, index != propertyInfos.Count - 1 ? "; " : ")");
- }
- return result.ToString();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement