Last active
July 29, 2021 07:27
-
-
Save relyky/105722cfae1041758347987dc2de8e32 to your computer and use it in GitHub Desktop.
c#, data annotation, 取得類別屬性(attribute) Display Name,
This file contains hidden or 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; | |
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