Skip to content

Commit

Permalink
Improve DisplayNameExtensions
Browse files Browse the repository at this point in the history
  • Loading branch information
VahidN committed Jun 29, 2024
1 parent 2e114e0 commit 8989b66
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/DNTCommon.Web.Core/DNTCommon.Web.Core.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Description>DNTCommon.Web.Core provides common scenarios' solutions for ASP.NET Core applications.</Description>
<VersionPrefix>5.3.5</VersionPrefix>
<VersionPrefix>5.3.6</VersionPrefix>
<Authors>Vahid Nasiri</Authors>
<TargetFrameworks>net8.0;net7.0;net6.0;</TargetFrameworks>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
Expand Down
47 changes: 39 additions & 8 deletions src/DNTCommon.Web.Core/MiscUtils/DisplayNameExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,18 @@ public static class DisplayNameExtensions
/// <summary>
/// Gets the DisplayName of the provided expression
/// </summary>
public static string GetDisplayName<TEntity>(this Expression<Func<TEntity>>? expression) => expression.GetDisplayName();
public static string GetDisplayName<TEntity>(this Expression<Func<TEntity, object?>>? expression)
{
var (_, memberExp) = GetPropertyInfo(expression);
var displayAttribute = memberExp.Member.GetCustomAttribute<DisplayAttribute>();

return displayAttribute is null ? string.Empty : displayAttribute.Name ?? memberExp.Member.Name;
}

/// <summary>
/// Gets the DisplayName of the provided expression
/// </summary>
public static string GetDisplayName(this Expression? expression)
public static string GetDisplayName<TEntity>(this Expression<Func<TEntity>>? expression)
{
var memberExp = expression.GetMemberExpression();

Expand All @@ -25,12 +31,7 @@ public static string GetDisplayName(this Expression? expression)

var displayAttribute = memberExp.Member.GetCustomAttribute<DisplayAttribute>();

if (displayAttribute is null)
{
return string.Empty;
}

return displayAttribute.Name ?? memberExp.Member.Name;
return displayAttribute is null ? string.Empty : displayAttribute.Name ?? memberExp.Member.Name;
}

/// <summary>
Expand Down Expand Up @@ -58,4 +59,34 @@ public static string GetDisplayName(this Expression? expression)

return null;
}

/// <summary>
/// Gets the PropertyInfo of the provided expression
/// </summary>
public static (PropertyInfo Info, MemberExpression MemberExp) GetPropertyInfo<TEntity>(
this Expression<Func<TEntity, object?>>? expression)
=> expression?.Body switch
{
null => throw new ArgumentNullException(nameof(expression)),
UnaryExpression { Operand: MemberExpression memberExp } => ((PropertyInfo)memberExp.Member, memberExp),
MemberExpression memberExp => ((PropertyInfo)memberExp.Member, memberExp),
_ => throw new ArgumentException($"The expression doesn't indicate a valid property. [ {expression} ]",
nameof(expression))
};

/// <summary>
/// Get the actual return type of expr
/// </summary>
public static Type? GetObjectType<TEntity>(this Expression<Func<TEntity, object?>>? expression)
{
if (expression is null)
{
return null;
}

return expression.Body.NodeType is ExpressionType.Convert or ExpressionType.ConvertChecked &&
expression.Body is UnaryExpression unary
? unary.Operand.Type
: expression.Body.Type;
}
}

0 comments on commit 8989b66

Please sign in to comment.