mirror of https://github.com/Squidex/squidex.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
50 lines
1.6 KiB
50 lines
1.6 KiB
// ==========================================================================
|
|
// Squidex Headless CMS
|
|
// ==========================================================================
|
|
// Copyright (c) Squidex UG (haftungsbeschraenkt)
|
|
// All rights reserved. Licensed under the MIT license.
|
|
// ==========================================================================
|
|
|
|
using System;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
using Squidex.Infrastructure.Reflection.Internal;
|
|
|
|
namespace Squidex.Infrastructure.Reflection.Equality
|
|
{
|
|
public sealed class ObjectComparer : IDeepComparer
|
|
{
|
|
private readonly PropertyAccessor[] propertyAccessors;
|
|
private readonly IDeepComparer valueComparer;
|
|
|
|
public ObjectComparer(IDeepComparer valueComparer, Type type)
|
|
{
|
|
propertyAccessors =
|
|
type.GetPublicProperties()
|
|
.Where(x => x.CanRead)
|
|
.Where(x => x.GetCustomAttribute<IgnoreEqualsAttribute>() == null)
|
|
.Select(x => new PropertyAccessor(x.DeclaringType!, x))
|
|
.ToArray();
|
|
|
|
this.valueComparer = valueComparer;
|
|
}
|
|
|
|
public bool IsEquals(object? x, object? y)
|
|
{
|
|
for (var i = 0; i < propertyAccessors.Length; i++)
|
|
{
|
|
var property = propertyAccessors[i];
|
|
|
|
var lhs = property.Get(x!);
|
|
var rhs = property.Get(y!);
|
|
|
|
if (!valueComparer.IsEquals(lhs, rhs))
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
|