Skip to content

Instantly share code, notes, and snippets.

@hagbarddenstore
Last active December 26, 2015 01:19
Show Gist options
  • Save hagbarddenstore/7070186 to your computer and use it in GitHub Desktop.
Save hagbarddenstore/7070186 to your computer and use it in GitHub Desktop.
namespace A
{
using System;
using System.Linq;
using System.ComponentModel;
public static class Program
{
public static void Main()
{
var ceo = new CEO();
var worker = new Worker();
Console.WriteLine("{0}", ceo.GetDisplayName());
Console.WriteLine("{0}", worker.GetDisplayName());
}
private static string GetDisplayName<T>(this T self)
{
var attribute = typeof(T).GetCustomAttributes(typeof(DisplayNameAttribute), false)
.OfType<DisplayNameAttribute>()
.FirstOrDefault();
return attribute != null ? attribute.DisplayName : "N/A";
}
}
public abstract class Employee
{
}
[DisplayName("Worker")]
public class Worker : Employee
{
}
public class CEO : Employee
{
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment