Skip to content

Instantly share code, notes, and snippets.

@relyky
Last active July 29, 2021 07:27
Show Gist options
  • Save relyky/105722cfae1041758347987dc2de8e32 to your computer and use it in GitHub Desktop.
Save relyky/105722cfae1041758347987dc2de8e32 to your computer and use it in GitHub Desktop.
c#, data annotation, 取得類別屬性(attribute) Display Name,
using System;
using System.Linq;
using System.ComponentModel.DataAnnotations;
public static class DBHelperClassExtensions
{
public static string DisplayNameOf(Type ty, string propertyName)
{
var field = ty.GetProperty(propertyName);
var attr = field.GetCustomAttributes(typeof(DisplayAttribute), false).FirstOrDefault() as DisplayAttribute;
return attr?.Name;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment