Add ReflectionExtensions for property lookup by column name

Introduced a static ReflectionExtensions class with a GetValueByColumnName extension method to retrieve property values by their [Column] attribute name. Also removed an unused folder reference from the project file.
This commit is contained in:
2026-03-26 13:16:04 +01:00
parent e96773f3c4
commit d8aa032a57
2 changed files with 19 additions and 1 deletions

View File

@@ -25,7 +25,6 @@
<ItemGroup> <ItemGroup>
<Folder Include="Common\Behaviors\Action\" /> <Folder Include="Common\Behaviors\Action\" />
<Folder Include="Common\Options\DbModel\" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@@ -0,0 +1,19 @@
using System.ComponentModel.DataAnnotations.Schema;
using System.Reflection;
namespace ReC.Domain.Extensions;
public static class ReflectionExtensions
{
/// <summary>
/// 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.
/// </summary>
public static object? GetValueByColumnName(this object obj, string columnName)
{
var property = obj.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance)
.FirstOrDefault(p => p.GetCustomAttribute<ColumnAttribute>()?.Name == columnName);
return property?.GetValue(obj);
}
}