#if NET namespace DigitalData.Core.Abstraction.Application; /// /// Provides extension methods for retrieving the value of an 'Id' property from objects. /// public static class EntityExtensions { /// /// Attempts to retrieve the value of the 'Id' property from the specified object. /// /// The expected type of the 'Id' property. /// The object from which to retrieve the 'Id' property. /// /// The value of the 'Id' property if it exists and is of type ; otherwise, default. /// public static TId? GetIdOrDefault(this object? obj) { var prop = obj?.GetType().GetProperty("Id"); return prop is not null && prop.GetValue(obj) is TId id ? id : default; } /// /// Retrieves the value of the 'Id' property from the specified object, or throws an exception if not found or of the wrong type. /// /// The expected type of the 'Id' property. /// The object from which to retrieve the 'Id' property. /// The value of the 'Id' property. /// /// Thrown if the object does not have a readable 'Id' property of type . /// public static TId GetId(this object? obj) => obj.GetIdOrDefault() ?? throw new InvalidOperationException($"The object of type '{obj?.GetType().FullName ?? "null"}' does not have a readable 'Id' property of type '{typeof(TId).FullName}'."); /// /// Tries to retrieve the value of the 'Id' property from the specified object. /// /// The expected type of the 'Id' property. /// The object from which to retrieve the 'Id' property. /// When this method returns, contains the value of the 'Id' property if found; otherwise, the default value for the type. /// /// true if the 'Id' property was found and is of type ; otherwise, false. /// public static bool TryGetId(object? obj, out TId id) { #pragma warning disable CS8601 id = obj.GetIdOrDefault(); #pragma warning restore CS8601 return id is not null; } /// /// Attempts to retrieve the value of the 'Id' property from the specified object. /// /// The object from which to retrieve the 'Id' property. /// /// The value of the 'Id' property if it exists; otherwise, null. /// public static object? GetIdOrDefault(this object? obj) { var prop = obj?.GetType().GetProperty("Id"); return prop?.GetValue(obj); } /// /// Retrieves the value of the 'Id' property from the specified object, or throws an exception if not found. /// /// The object from which to retrieve the 'Id' property. /// The value of the 'Id' property. /// /// Thrown if the object does not have a readable 'Id' property. /// public static object GetId(this object? obj) => obj.GetIdOrDefault() ?? throw new InvalidOperationException($"The object of type '{obj?.GetType().FullName ?? "null"}' does not have a readable 'Id' property."); /// /// Tries to retrieve the value of the 'Id' property from the specified object. /// /// The object from which to retrieve the 'Id' property. /// /// When this method returns, contains the value of the 'Id' property if found; otherwise, null. /// /// /// true if the 'Id' property was found; otherwise, false. /// public static bool TryGetId(object? obj, out object id) { #pragma warning disable CS8601 id = obj.GetIdOrDefault(); #pragma warning restore CS8601 return id is not null; } } #endif