Created
June 8, 2021 17:58
-
-
Save mxmissile/504cf5e5fe693be83b61cfa108b6a662 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@using System.Reflection | |
@using System.Linq.Expressions; | |
@using System.ComponentModel | |
@using System.ComponentModel.DataAnnotations | |
@typeparam T | |
<label for="@control">@label</label> | |
@code { | |
protected override void OnInitialized() | |
{ | |
base.OnInitialized(); | |
var expression = (MemberExpression)For.Body; | |
control = expression.Member.Name; | |
if (expression.Member.GetCustomAttribute(typeof(DisplayAttribute)) is DisplayAttribute a1) | |
{ | |
label = a1.Name; | |
return; | |
} | |
// just in case DisplayName is used instead | |
if (expression.Member.GetCustomAttribute(typeof(DisplayNameAttribute)) is DisplayNameAttribute a2) | |
{ | |
label = a2.DisplayName; | |
return; | |
} | |
label = control; | |
} | |
[Parameter] public Expression<Func<T>> For { get; set; } | |
private string label; | |
private string control; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment