// Copyright (c) MudBlazor 2021 // MudBlazor licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. using System.Linq.Expressions; using Connected.Annotations; namespace Connected { public static class ExpressionExtensions { public static string GetFullPathOfMember(this Expression> property) { var resultingString = string.Empty; var p = property.Body as MemberExpression; while (p != null) { if (p.Expression is MemberExpression) { resultingString = p.Member.Name + (resultingString != string.Empty ? "." : string.Empty) + resultingString; } p = p.Expression as MemberExpression; } return resultingString; } /// /// Returns the display name attribute of the provided field property as a string. If this attribute is missing, the member name will be returned. /// public static string GetLabelString(this Expression> expression) { var memberExpression = (MemberExpression)expression.Body; var propertyInfo = memberExpression.Expression?.Type.GetProperty(memberExpression.Member.Name); return propertyInfo?.GetCustomAttributes(typeof(LabelAttribute), true).Cast().FirstOrDefault()?.Name ?? string.Empty; } } }