Skip to content

Instantly share code, notes, and snippets.

@mxmissile
Created June 8, 2021 17:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mxmissile/504cf5e5fe693be83b61cfa108b6a662 to your computer and use it in GitHub Desktop.
Save mxmissile/504cf5e5fe693be83b61cfa108b6a662 to your computer and use it in GitHub Desktop.
@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