AdministrationWindow: Use GetHashCode() to compare default/original values

This commit is contained in:
2024-03-18 16:10:31 +01:00
parent 729d2fd76c
commit 51e345f1fd
3 changed files with 45 additions and 29 deletions

View File

@ -18,7 +18,9 @@ using System.Text.Json.Nodes;
using System.IO;
using MailKit.Net.Smtp;
using MailKit.Security;
using OpenTK.Compute.OpenCL;
using Microsoft.EntityFrameworkCore;
using System.Reflection;
using System.Collections;
namespace Elwig.Helpers {
public static partial class Utils {
@ -432,5 +434,19 @@ namespace Elwig.Helpers {
await client.AuthenticateAsync(username, password);
return client;
}
public static int GetEntityIdentifier(object? obj) {
if (obj == null) {
return 0;
} else if (obj is IEnumerable list) {
var arr = list.Cast<object>().Select(o => GetEntityIdentifier(o)).ToArray();
return ((IStructuralEquatable)arr).GetHashCode(EqualityComparer<int>.Default);
} else if (obj.GetType().GetCustomAttribute(typeof(PrimaryKeyAttribute), false) is not PrimaryKeyAttribute pkAttr) {
return obj.GetHashCode();
} else {
var pk = pkAttr.PropertyNames.Select(name => obj.GetType().GetProperty(name)!.GetValue(obj)?.GetHashCode() ?? 0).ToArray();
return ((IStructuralEquatable)pk).GetHashCode(EqualityComparer<int>.Default);
}
}
}
}