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:
@@ -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>
|
||||||
|
|||||||
19
src/ReC.Domain/Extensions/ReflectionExtensions.cs
Normal file
19
src/ReC.Domain/Extensions/ReflectionExtensions.cs
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user