You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
39 lines
1.4 KiB
39 lines
1.4 KiB
using System;
|
|
using System.ComponentModel.DataAnnotations;
|
|
using System.Linq.Expressions;
|
|
using System.Reflection;
|
|
|
|
namespace Infrastructure.Extensions
|
|
{
|
|
public static class ExpressionExtensions
|
|
{
|
|
public static string GetPropertyName(this Expression<Func<object, object>> expression)
|
|
{
|
|
if (expression is null)
|
|
{
|
|
throw new ArgumentNullException(nameof(expression));
|
|
}
|
|
return (expression.Body as MemberExpression).Member.Name;
|
|
}
|
|
|
|
public static string MemberName<T, V>(this Expression<Func<T, V>> expression)
|
|
{
|
|
if (expression.Body is not MemberExpression memberExpression)
|
|
throw new InvalidOperationException("Expression must be a member expression");
|
|
|
|
return memberExpression.Member.Name;
|
|
}
|
|
|
|
public static string GetDisplayAttributeValue<T>(this Expression<Func<T, object>> expression)
|
|
{
|
|
if (expression is null)
|
|
{
|
|
throw new ArgumentNullException(nameof(expression));
|
|
}
|
|
var memberExpression = expression.Body as MemberExpression;
|
|
|
|
var displayAttribute = memberExpression.Member.GetCustomAttribute<DisplayAttribute>();
|
|
return displayAttribute == null ? memberExpression.Member.Name : displayAttribute.Name;
|
|
}
|
|
}
|
|
} |