using System.Reflection; namespace Toolkit.Foundation; public static class ObjectExtensions { public static T Also(this T value, Action action) { action(value); return value; } public static TAttribute? GetAttribute(this object obj) where TAttribute : Attribute { Type type = obj.GetType(); if (type.GetCustomAttribute(true) is TAttribute attribute) { return attribute; } return null; } public static IEnumerable GetAttributes(this object obj) where TAttribute : Attribute { Type type = obj.GetType(); if (type.GetCustomAttributes(true) is IEnumerable attributes) { return attributes; } return Enumerable.Empty(); } public static object? GetPropertyValue(this object obj, Func selector) { Type type = obj.GetType(); object? key = selector(); if (type.GetProperty($"{key}") is PropertyInfo property && property.GetValue(obj) is { } value) { return value; } return null; } }