Get class DisplayName attribute value
using your example I got it working doing this:
var displayName = typeof(Opportunity)
.GetCustomAttributes(typeof(DisplayNameAttribute), true)
.FirstOrDefault() as DisplayNameAttribute;
if (displayName != null)
Console.WriteLine(displayName.DisplayName);
This outputted "Opportunity".
Or for the more generic way you seem to be doing it:
public static string GetDisplayName<T>()
{
var displayName = typeof(T)
.GetCustomAttributes(typeof(DisplayNameAttribute), true)
.FirstOrDefault() as DisplayNameAttribute;
if (displayName != null)
return displayName.DisplayName;
return "";
}
Usage:
string displayName = GetDisplayName<Opportunity>();
GetCustomAttributes()
returns an object[]
, so you need to apply the specific cast first before accessing the required property values.