Make GetValueByColumnName a generic extension method

Refactored GetValueByColumnName to use a generic type parameter constrained to class types. This enhances type safety and enables better type inference and static analysis when accessing property values by column name.
This commit is contained in:
2026-03-26 13:42:45 +01:00
parent d8aa032a57
commit 56730c0d4e

View File

@@ -9,7 +9,7 @@ public static class ReflectionExtensions
/// Gets the value of a property by its column name defined in <see cref="ColumnAttribute"/>. /// Gets the value of a property by its column name defined in <see cref="ColumnAttribute"/>.
/// Returns <c>null</c> if no property with the given column name exists. /// Returns <c>null</c> if no property with the given column name exists.
/// </summary> /// </summary>
public static object? GetValueByColumnName(this object obj, string columnName) public static object? GetValueByColumnName<T>(this T obj, string columnName) where T : class
{ {
var property = obj.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance) var property = obj.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance)
.FirstOrDefault(p => p.GetCustomAttribute<ColumnAttribute>()?.Name == columnName); .FirstOrDefault(p => p.GetCustomAttribute<ColumnAttribute>()?.Name == columnName);