fork(3) download
  1. using System;
  2. using System.Reflection;
  3.  
  4. public class Test
  5. {
  6. public static void Main()
  7. {
  8. var res = typeof(Books).GetField("IndianInTheCupboard").GetCustomAttribute<BookDetails>(false).Author;
  9. Console.WriteLine(res);
  10. }
  11. }
  12.  
  13. [AttributeUsage(AttributeTargets.Field)]
  14. public class BookDetails : Attribute
  15. {
  16. public string Author { get; }
  17. public int YearPublished { get; }
  18.  
  19. public BookDetails(string author, int yearPublished)
  20. {
  21. Author = author;
  22. YearPublished = yearPublished;
  23. }
  24. }
  25.  
  26. public enum Books
  27. {
  28. [BookDetails("Jack London", 1906)]
  29. WhiteFange,
  30.  
  31. [BookDetails("Herman Melville", 1851)]
  32. MobyDick,
  33.  
  34. [BookDetails("Lynne Reid Banks", 1980)]
  35. IndianInTheCupboard
  36.  
  37. }
Success #stdin #stdout 0.01s 131200KB
stdin
Standard input is empty
stdout
Lynne Reid Banks